Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Shell test Command

Shell test Command

test The command is a built-in conditional judgment tool in Shell, used to evaluate expressions and return a Boolean value (true/false); it is usually used together with the if statement, and is the foundation of logic control in Shell scripts.

The test command in Shell is used to check whether a condition holds; it can perform tests on numbers, strings, and files.

Syntax format

test EXPRESSION

# 或

[ EXPRESSION ]  # 注意方括号内必须有空格


File test operations

test The command is most commonly used to check file attributes; the following are common file test options:

Operator Description Example
-e Whether a file exists [ -e file.txt ]
-f is a regular file [ -f /path/to/file ]
-d Is a directory [ -d /path/to/dir ]
-r is readable [ -r file.txt ]
-w is writable [ -w file.txt ]
-x is executable [ -x script.sh ]
-s file size > 0 [ -s logfile ]
-L is a symbolic link [ -L symlink ]

Example script:

Example

#!/bin/bash

file

=

"/etc/passwd"

if

[

-e

"$file"

]

;

then

   

echo

"$file exists"

   

if

[

-r

"$file"

]

;

then

       

echo

"and is readable"

   

fi

else

   

echo

"$file does not exist"

fi

The output is:

/etc/passwd 存在

并且可读


String comparison

test Multiple string comparison methods are provided:

Operator Description Example
-z STRING string is empty [ -z "$var" ]
-n STRING string is not empty [ -n "$var" ]
STRING1 = STRING2 Strings are equal [ "$var1" = "$var2" ]
STRING1 != STRING2 strings are not equal [ "$var1" != "$var2" ]

Important Note: String variables should always be enclosed in double quotes to prevent syntax errors caused by empty variables.

Example:

Example

#!/bin/bash

read

-p

"Enter username: "

username

if

[

-z

"$username"

]

;

then

   

echo

"Error: Username cannot be empty"

   

exit

1

elif

[

"$username"

=

"root"

]

;

then

   

echo

"Warning: Using the root account is not recommended"

else

   

echo

"Welcome, $username"

fi

After running, we enter runoob in the terminal, and the output is similar to the following:

输入用户名: runoob

欢迎, runoob


Numeric comparison

For numeric comparisons,test different operators are used:

Operator Description Example
-eq Equal [ "$a" -eq "$b" ]
-ne Not equal [ "$a" -ne "$b" ]
-gt Greater than [ "$a" -gt "$b" ]
-ge Greater than or equal to [ "$a" -ge "$b" ]
-lt Less than [ "$a" -lt "$b" ]
-le Less than or equal to [ "$a" -le "$b" ]

Example:

Example

#!/bin/bash

read

-p

"Enter age: "

age

if

[

"$age"

-lt

0

]

;

then

   

echo

"Age cannot be negative"

elif

[

"$age"

-lt

18

]

;

then

   

echo

"Minor"

elif

[

"$age"

-ge

18

]

&&

[

"$age"

-lt

60

]

;

then

   

echo

"Adult"

else

   

echo

"Senior citizen"

fi

After running, we enter 12 in the terminal, and the output is similar to the following:

输入年龄: 12

未成年人


Logical operators

test supports logical combinations:

Operator Description Example
! Logical NOT [ ! -f "$file" ]
-a Logical AND [ "$a" -eq 1 -a "$b" -eq 2 ]
-o Logical OR [ "$a" -eq 1 -o "$b" -eq 2 ]

Modern recommended syntax: When using && and || replaces -a and -o, which is more in line with the POSIX standard:

Example

[

"$a"

-eq

1

]

&&

[

"$b"

-eq

2

]

 

The difference between

[

"$a"

-eq

1

]

||

[

"$b"

-eq

2

]

 

or


Advanced Usage: [[ ]] and (( ))

Bash provides more powerful test syntax:

Double Brackets [[ ]]

Arithmetic Comparison (( ))

Example:

Example

if

[

[

"$file"

==

.log

]

]

;

then

   

echo

"This is a log file"

fi

if

(

(

$count

10

)

)

;

then

   

echo

"Quantity exceeds 10"

fi


Practical Application Examples

1. Check whether a service is running

Example

#!/bin/bash

service

=

"nginx"

if

systemctl is-active

--quiet

"$service"

;

then

   

echo

"$service is running"

else

   

echo

"$service is not running"

   

You can add a command to start the service

fi

2. Backup file check

Example

#!/bin/bash

backup_file

=

"/backups/data_$(date +%Y%m%d).tar.gz"

if

[

!

-f

"$backup_file"

]

;

then

   

echo

"Error: Backup file $backup_file does not exist"

   

exit

1

elif

[

!

-s

"$backup_file"

]

;

then

   

echo

"Warning: Backup file is empty"

else

   

echo

"Backup verification successful"

fi


Common Mistakes and Debugging Tips

  1. Missing spaces:[ "$a"="$b" ] is incorrect; the correct form is [ "$a" = "$b" ]
  2. Unquoted variables:[ -f $file ] should be [ -f "$file" ]
  3. Confusing string and numeric comparisons: When using = compares strings,-eq compares numbers

Debugging tip: Add at the beginning of the script set -x to enable debug mode, or use echo to print test expressions:

Example

echo

"Test expression: [ $a -eq $b ]"

[

"$a"

-eq

"$b"

]

&&

echo

"True"

||

echo

"False"