How to use the command '[[ ]]' (with examples)
The [[ ]]
command is a conditional command in Bash that allows you to perform various tests and evaluations. It can be used to check conditions, compare values, and validate files or directories. This command is commonly used in shell scripting to make decisions based on the outcome of these tests.
Use case 1: Testing if a variable is equal/not equal to a specified string
Code:
[[ $variable ==|!= "string" ]]
Motivation: This use case is useful when you need to compare the value of a variable to a specific string. You can easily determine whether the variable matches the string or not, and based on the result, take appropriate actions in your script.
Explanation:
$variable
is the variable you want to test.==|!=
is the operator used to check if the variable is equal (==
) or not equal (!=
) to the specified string."string"
is the string you want to compare the variable with.
Example output:
If $variable
contains the value “example”, the command [[ $variable == "example" ]]
will return a status of 0 (true). On the other hand, if $variable
contains a different value, the command will return a status of 1 (false).
Use case 2: Testing if a string conforms to a glob/regex pattern
Code:
[[ $variable ==|=~ pattern ]]
Motivation: When you need to validate whether a given string matches a specific pattern defined by a glob or regex, this use case comes in handy. It allows you to check if the string conforms to the specified pattern and take actions accordingly.
Explanation:
$variable
is the variable containing the string you want to test.==|=~
is the operator used for pattern matching.==
is used for matching a glob pattern whereas=~
is used for matching a regular expression (regex) pattern.pattern
is the glob or regex pattern you want to compare the variable with.
Example output:
If $variable
contains the value “example”, the command [[ $variable == "exa*" ]]
will return a status of 0 (true) since the string matches the glob pattern. However, if the string does not match the pattern, the command will return a status of 1 (false).
Use case 3: Testing if a variable is equal to, not equal to, greater than, less than, greater than or equal to, or less than or equal to a specified number
Code:
[[ $variable -eq|ne|gt|lt|ge|le integer ]]
Motivation: This use case is relevant when you need to perform numerical comparisons on variables. It allows you to check if a variable is equal to, not equal to, greater than, less than, greater than or equal to, or less than or equal to a specified number, and make decisions based on the outcome.
Explanation:
$variable
is the variable you want to compare.-eq|ne|gt|lt|ge|le
are the operators used for numerical comparisons.-eq
is used for equal to,ne
for not equal to,gt
for greater than,lt
for less than,ge
for greater than or equal to, andle
for less than or equal to.integer
is the number you want to compare the variable with.
Example output:
If $variable
contains the value 5, the command [[ $variable -gt 3 ]]
will return a status of 0 (true) as the variable is greater than the specified number. Conversely, if $variable
is less than or equal to 3, the command will return a status of 1 (false).
Use case 4: Testing if a variable has a non-empty value
Code:
[[ -n $variable ]]
Motivation: This use case is helpful when you want to check whether a variable has a value assigned to it or if it is empty. It enables you to handle different scenarios based on the presence or absence of a value in the variable.
Explanation:
-n
is the operator used to check if the variable has a non-empty value.$variable
is the variable you want to test.
Example output:
If $variable
contains any value, the command [[ -n $variable ]]
will return a status of 0 (true) since the variable has a non-empty value. On the other hand, if the variable is empty, the command will return a status of 1 (false).
Use case 5: Testing if a variable has an empty value
Code:
[[ -z $variable ]]
Motivation: When you need to determine if a variable is empty, this use case is extremely useful. It allows you to easily identify whether the variable has been assigned a value or if it remains empty.
Explanation:
-z
is the operator used to check if the variable has an empty value.$variable
is the variable you want to test.
Example output:
If $variable
is an empty string, the command [[ -z $variable ]]
will return a status of 0 (true) as the variable is empty. However, if the variable contains any value, the command will return a status of 1 (false).
Use case 6: Testing if a file exists
Code:
[[ -f path/to/file ]]
Motivation: This use case is valuable when you want to determine whether a specific file exists or not. It enables you to perform actions based on the existence or absence of the file.
Explanation:
-f
is the operator used to check if the specified path corresponds to a regular file.path/to/file
is the path of the file you want to test.
Example output:
If the file at path/to/file
exists, the command [[ -f path/to/file ]]
will return a status of 0 (true). However, if the file does not exist or if the path points to a directory or another type of file, the command will return a status of 1 (false).
Use case 7: Testing if a directory exists
Code:
[[ -d path/to/directory ]]
Motivation: When you need to verify the existence of a specific directory, this use case becomes handy. It allows you to check if the given directory path is valid and take actions accordingly.
Explanation:
-d
is the operator used to check if the specified path corresponds to a directory.path/to/directory
is the path of the directory you want to test.
Example output:
If the directory at path/to/directory
exists, the command [[ -d path/to/directory ]]
will return a status of 0 (true). In case the directory does not exist or if the path points to a file, the command will return a status of 1 (false).
Use case 8: Testing if a file or directory exists
Code:
[[ -e path/to/file_or_directory ]]
Motivation: This use case is applicable when you want to check if a file or directory exists at a specific path. It allows you to handle different scenarios based on the presence or absence of the file or directory.
Explanation:
-e
is the operator used to check if the specified path exists.path/to/file_or_directory
is the path of the file or directory you want to test.
Example output:
If the file or directory at path/to/file_or_directory
exists, the command [[ -e path/to/file_or_directory ]]
will return a status of 0 (true). On the other hand, if neither a file nor a directory exists at the specified path, the command will return a status of 1 (false).
Conclusion:
The [[ ]]
command in Bash is a powerful tool for performing conditional evaluations. With its various operators and options, it enables you to check conditions, compare values, and validate files or directories. By understanding and utilizing these use cases, you can make your shell scripts more robust and flexible.