Brackets in Shell (including single brackets and double brackets) can be used for testing certain conditions:

  • Arithmetic comparison, for example whether a variable is 0,[ $var -eq 0 ]。
  • File attribute testing, for example whether a file exists,[ -e $var ], whether it is a directory,[ -d $var ]。
  • String comparison, for example whether two strings are the same,[[ $var1 = $var2 ]]。

[] can often be replaced by the test command, which will be introduced later.

Arithmetic Comparison

Perform arithmetic condition judgment on variables or values:

[ $var -eq 0 ]  # 当 $var 等于 0 时,返回真
[ $var -ne 0 ]  # 当 $var 不等于 0 时,返回真

Note that there must be a space between [ and ] and the operands, otherwise an error will be reported. For example, the following will cause an error:

[$var -eq 0 ]  或 [ $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

You can combine multiple conditions for testing with -a (and) or -o (or):

[ $var1 -ne 0 -a $var2 -gt 2 ]  # 使用逻辑与 -a
[ $var1 -ne 0 -o $var2 -gt 2 ]  # 使用逻辑或 -o

File System Attribute Testing

Use different condition flags to test different file system attributes.

Operator Meaning
[ -f $file_var ] Returns true if the variable $file_var is a normal file path or file name (file)
[ -x $var ] Returns true if the file contained in the variable $var is executable (execute)
[ -d $var ] Returns true if the file contained in the variable $var is a directory (directory)
[ -e $var ] Returns true if the file contained in the variable $var exists (exist)
[ -c $var ] Returns true if the file contained in the variable $var is a character device file path (character)
[ -b $var ] Returns true if the file contained in the variable $var is a block device file path (block)
[ -w $var ] Returns true if the file contained in the variable $var is writable (write)
[ -r $var ] Returns true if the file contained in the variable $var is readable (read)
[ -L $var ] Returns true if the file contained in the variable $var is a symbolic link (link)

The usage method is as follows:

fpath="/etc/passwd"
if [ -e $fpath ]; then
  echo File exits;
else
  echo Does not exit;
fi

String Comparison

When performing string comparison, it is best to use double brackets [[ ]]. Because single brackets may cause some errors, it is best to avoid them.

Check whether two strings are the same:

[[ $str1 = $str2 ]]

Returns true when str1 equals str2. That is, the text contained in str1 and str2 is the same. The single equal sign can also be written as a double equal sign, meaning the above string comparison is equivalent to [[ $str1 == $str2 ]].

Note that there is a space before and after =. If you forget to add the space, it becomes an assignment statement rather than a comparison relationship.

Other comparison cases for strings:

Operator Meaning
[[ $str1 != $str2 ]] Returns true if str1 and str2 are not the same
[[ -z $str1 ]] Returns true if str1 is an empty string
[[ -n $str1 ]] Returns true if str1 is a non-empty string

Using the logical operators && and ||, you can easily combine multiple conditions, for example:

str1="Not empty"
str2=""
if [[ -n $str1 ]] && [[ -z $str2 ]];
then
  echo str1 is nonempty and str2 is empty string.
fi

The test command can also be used to perform condition detection. Using test can avoid using too many brackets. The test conditions in [] can also be accomplished through test.

if [ $var -eq 0 ]; then echo "True"; fi

Equivalent to:

if test $var -eq 0; then echo "True"; fi

Author: liuchengxu_

Original: https://blog.csdn.net/simple_the_best/article/details/75915767