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

Linux test Command

Linux test Command

Linux Command Reference Linux Command Reference


test is a built-in command in Linux/Unix systems for conditional judgment, used mainly in shell scripts for various tests and comparisons. It can check file attributes and compare strings and numbers, making it an indispensable tool for shell scripting.


Basic Syntax



test EXPRESSION

# Or use bracket format (more common)

[ EXPRESSION ]

Note: When using the bracket format, there must be spaces between the expression and the brackets, i.e. [空格EXPRESSION空格]


Main Feature Categories

1. File Tests

Check various attributes of files or directories:

Expression Meaning
-e 文件 Whether a file exists
-f 文件 Is a regular file (not a directory or device file)
-d 文件 Is a directory
-s 文件 File size is not empty
-r 文件 File is readable
-w 文件 File is writable
-x 文件 File is executable
-L 文件 File is a symbolic link

Example:

Example

if

[

-f

"/etc/passwd"

]

;

then

   

echo

"This is a regular file"

fi


2. String Comparisons

Compare the relationship between two strings:

Expression Meaning
-z "字符串" String length is 0
-n "字符串" String length is not 0
"串1" = "串2" Strings are equal
"串1" != "串2" Strings are not equal

Example:

Example

read

-p

"Enter username: "

username

if

[

-z

"$username"

]

;

then

   

echo

"Username cannot be empty"

fi


3. Numeric Comparisons

Compare the magnitude relationship between two integers:

Expression Meaning
n1 -eq n2 Equal (equal)
n1 -ne n2 Not equal (not equal)
n1 -gt n2 Greater than (greater than)
n1 -ge n2 Greater than or equal to
n1 -lt n2 Less than (less than)
n1 -le n2 Less than or equal to

Example:

Example

if

[

$

(

id

-u

)

-eq

0

]

;

then

   

echo

"Currently the root user"

fi


4. Logical Operators

Combine multiple test conditions:

operator Meaning Example
! Logical NOT [ ! -f file ]
-a Logical AND [ -f file -a -r file ]
-o Logical OR [ -d dir -o -f file ]

Modern recommended syntax(using && and ||):

Example

[

-f

file

]

&&

[

-r

file

]

 

File exists and is readable

[

-d

dir

]

||

[

-f

file

]

 

Is a directory or a file


Advanced usage

1. Compound Condition Checks

Example

if

[

-f

"/etc/passwd"

-a

-r

"/etc/passwd"

]

;

then

   

echo

"File exists and is readable"

fi

2. Combined with if Statements

Example

if

[

"$1"

=

"start"

]

;

then

   

echo

"Starting service..."

elif

[

"$1"

=

"stop"

]

;

then

   

echo

"Stopping service..."

else

   

echo

"Invalid argument"

fi

3. Use in Variable Assignment

Example

[

-z

"$PATH"

]

&&

PATH

=

"/usr/bin:/bin"

 

Set a default value if PATH is empty


Common errors and caveats

Spacing Issues:

Example

[

"$var"

=

"value"

]

 

Correct

[

"$var"

=

"value"

]

   

Wrong: missing spaces

Quoting String Variables:

Example

[

-n

"$var"

]

 

Correct: prevents errors when a variable is empty

[

-n

$var

]

   

Errors occur when $var is empty

Numeric vs. String Comparisons:

Example

[

"10"

-gt

"2"

]

 

Correct: numeric comparison

[

"10"

"2"

]

   

Wrong: this is a string comparison (lexicographic)

File Test Paths:

Example

[

-f

"$file"

]

   

Correct: variable wrapped in quotes

[

-f

$file

]

     

Errors occur when the path contains spaces


Practical Application Examples

Example 1: Check Whether a Backup Directory Exists

Example

backup_dir

=

"/var/backups"

if

[

!

-d

"$backup_dir"

]

;

then

   

mkdir

-p

"$backup_dir"

   

echo

"Creating backup directory: $backup_dir"

fi

Example 2: Validate User Input

Example

read

-p

"Please enter your age: "

age

if

[

"$age"

-lt

0

-o

"$age"

-gt

120

]

;

then

   

echo

"Invalid age input"

   

exit

1

fi

Example 3: Service Startup Script

Example

#!/bin/bash

if

[

"$1"

=

"start"

]

;

then

   

if

[

-f

"/var/run/service.pid"

]

;

then

       

echo

"Service is already running"

   

else

       

echo

"Starting service..."

       

Launch command

   

fi

elif

[

"$1"

=

"stop"

]

;

then

   

Stop logic

else

   

echo

"Usage: $0 {start|stop}"

fi


Summary

test The command is a fundamental tool in shell script programming; mastering it lets you:

Remember the key points:

  1. The bracket format is more common, but watch the spaces
  2. Wrap variable references in double quotes
  3. Distinguish string comparison from numeric comparison operators
  4. Use the modern && and || replaces -a and -o

Linux Command Reference Linux Command Reference