Summary of the Use of Brackets in Shell
Category Programming Techniques
Brackets in Shell (including single and double brackets) can be used for testing some conditions:
Arithmetic comparisons, such as whether a variable is 0,
[ $var -eq 0 ]
.File attribute tests, such as whether a file exists,
[ -e $var ]
, or whether it is a directory,[ -d $var ]
.String comparisons, such as whether two strings are the same,
[[ $var1 = $var2 ]]
.
Brackets [] can often be replaced with the test command, which is introduced later.
Arithmetic Comparison
Arithmetic condition judgment for variables or values:
[ $var -eq 0 ] # Returns true when $var equals 0
[ $var -ne 0 ] # Returns true when $var is not equal to 0
It is important to note that there must be a space between the brackets [ ] and the operands, otherwise an error will occur. For example, the following will result in an error:
[$var -eq 0 ] or [ $var -ne 0]
Other comparison operators:
Operator | Meaning |
---|---|
-gt | Greater than |
-lt | Less than |
-ge | Greater than or equal to |
-le | Less than or equal to |
Multiple conditions can be tested by combining them with -a (and) or -o (or):
[ $var1 -ne 0 -a $var2 -gt 2 ] # Using logical and -a
[ $var1 -ne 0 -o $var2 -gt 2 ] # Using logical or -o
File System Attribute Tests
Different condition flags are used to test different file system attributes.
Operator | Meaning |
---|---|
[ -f $file_var ] | If the variable $file_var is a normal file path or file name, returns true |
[ -x $var ] | If the file contained in the variable $var is executable, returns true |
[ -d $var ] | If the file contained in the variable $var is a directory, returns true |
[ -e $var ] | If the file contained in the variable $var exists, returns true |
[ -c $var ] | If the file contained in the variable $var is a character device file, returns true |
[ -b $var ] | If the file contained in the variable $var is a block device file, returns true |
[ -w $var ] | If the file contained in the variable $var is writable, returns true |
[ -r $var ] | If the file contained in the variable $var is readable, returns true |
[ -L $var ] | If the variable $var contains a symbolic link, returns true |
Usage example:
fpath="/etc/passwd"
if [ -e $fpath ]; then
echo File exists;
else
echo Does not exist;
fi
String Comparison
When comparing strings, it is best to use double brackets [[ ]]. This is because single brackets may cause some errors, so it is best to avoid them.
Check if two strings are the same:
[[ $str1 = $str2 ]]
Returns true when str1 equals str2, meaning that the text contained in str1 and str2 is the same. The single equals sign can also be written as a double equals sign, which means the string comparison above is equivalent to [[ $str1 == $str2 ]].
Note that there is a space around the equals sign; if the space is omitted, it becomes an assignment statement, not a comparison.
Other string comparison cases:
Operator | Meaning |
---|---|
[[ $str1 != $str2 ]] | Returns true if str1 is not the same as str2 |
[[ -z $str1 ]] | Returns true if str1 is an empty string |
[[ -n $str1 ]] | Returns true if str1 is a non-empty string |
Logical operators && and || can be used to easily combine multiple conditions, for example:
str1="Not empty"
str2=""
if [[ -n $str1 ]] && [[ -z $str2 ]];
then
echo str1 is nonempty and str2 is an empty string.
fi
The test command can also be used to perform conditional checks. Using test can avoid using too many brackets, and the test conditions in [] can also be completed using test.
if [ $var -eq 0 ]; then echo "True"; fi
Is equivalent to:
``` if test