Testing results using the TEST command in Linux

Home > Search > Linux commands
  by

The test command can be used for a variety of tests, such as comparing string "a" to string "b", comparting integers, checking if a file or directory exists, or checking if a file has certain permissions. There a two popular ways to determine the result of the test, exit codes and command line lists.

 


Exit codes

echo $? will display the exit code of the last run command. Exit code 0 indicates success.

[john.doe@server1 ~]# echo $?
0

 

Exit code 1 indicates failure.

[john.doe@server1 ~]# echo $?
1

 


Command line lists

Command line lists is the use of && or || in a single command.

 


Comparing string "a" to string "b"

The test command can be used to compare string "a" to string "b" to determine if the outcome of the comaprison is true or false. Exit code 0 will be displayed if string "a" equals "string "b".

[john.doe@server1 ~]# test "abc" = "abc"
[john.doe@server1 ~]# echo $?
0

 

Exit code 1 will be displayed if string "a" does not equal string "b".

[john.doe@server1 ~]# test "abc" = "123"
[john.doe@server1 ~]# echo $?
1

 

Command line lists can be used to display true or false instead of 0 or 1. True will be displayed if string "a" does equal string "b".

[john.doe@server1 ~]# test "abc" = "abc" && echo true || echo false
true

 

False will be displayed if string "a" does not equal string "b".

[john.doe@server1 ~]# test "abc" = "123" && echo true || echo false
false

 


Comparing integer "a" to integer "b"

The test command can be used to compare integer "a" to integer "b" to determine if the outcome of the comaprison is true or false. In this example, exit code 0 (true) will be displayed because integer "2" is greater than integer "1".

[john.doe@server1 ~]# test "2" -gt "1"
[john.doe@server1 ~]# echo $?
0

 


If directory exists

The -d option can be used to test if a directory exists. Exit code 0 will be displayed if example.file exists. 1 will be displayed if example.file does not exist.

[john.doe@server1 ~]# test -e /home/john.doe
[john.doe@server1 ~]# echo $?
0

 


If file exists

The -e option can be used to test if a file exists. Exit code 0 will be displayed if example.file exists. 1 will be displayed if example.file does not exist.

[john.doe@server1 ~]# test -e /home/john.doe/example.file
[john.doe@server1 ~]# echo $?
0

 


If regular file exists

The -f option can be used to test if a regular file exists. Exit code 0 will be displayed if example.file exists, and the file is a regular file. 1 will be displayed if example.file does not exist, or if the file is not a regular file.

[john.doe@server1 ~]# test -f /home/john.doe/example.file
[john.doe@server1 ~]# echo $?
0

 


If file not empty

The -s option can be used to test if a file exists, and the file is greater than 0 bytes. Let's say example.file is 0 bytes.

[john.doe@server1 ~]# ls -l /home/john.doe
-rwxr-x---. 1 john.doe john.doe 0 Apr 14 12:24 example.file

 

In this scenario, exit code 1 (false) will be displayed, because example.file is 0 bytes.

[john.doe@server1 ~]# test -s /home/john.doe/example.file
[john.doe@server1 ~]# echo $?
0

 


If file exists with read permission

The -r option can be used to test if a file exists and the file has the read permission. Let's say example.file does not have the read permission.

[john.doe@server1 ~]# ls -l /home/john.doe
--wx------. 1 john.doe john.doe 0 Apr 14 12:24 example.file

 

In this scenario, exit code 1 (false) will be displayed, because example.file does not have the read permission.

[john.doe@server1 ~]# test -r /home/john.doe/example.file
[john.doe@server1 ~]# echo $?
1

 


If file exists with write permission

The -w option can be used to test if a file exists and the file has the write permission. Let's say example.file does not have the write permission.

[john.doe@server1 ~]# ls -l /home/john.doe
-r-x------. 1 john.doe john.doe 0 Apr 14 12:24 example.file

 

In this scenario, exit code 1 (false) will be displayed, because example.file does not have the write permission.

[john.doe@server1 ~]# test -w /home/john.doe/example.file
[john.doe@server1 ~]# echo $?
1

 


If file exists with sticky bit

The -k option can be used to test if a file exists and the file has the sticky bit. Let's say example.file does have the sticky bit.

[john.doe@server1 ~]# ls -l /home/john.doe
-r-xr-xr-T. 1 john.doe john.doe 0 Apr 14 12:24 example.file

 

In this scenario, exit code 0 (true) will be displayed, because example.file does have the sticky bit.

[john.doe@server1 ~]# test -k /home/john.doe/example.file
[john.doe@server1 ~]# echo $?
0

 


If file is a symbolic link

The -h or -L option can be used to test if a file exists and the file is a symbolic link. For example, lets say the /etc/localtime file is symbolically linked to the /usr/share/zoneinfo/US/Central file.

[john.doe@server1 ~]# ls -l /etc/localtime
lrwxrwxrwx. 1 root root 37 Aug 30 2016 /etc/localtime -> ../usr/share/zoneinfo/US/Central

 

In this scenario, exit code 0 (true) will be displayed, because the /etc/localtime file is symbolically linked to another file.

[john.doe@server1 ~]# test -h /etc/localtime


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments