Bootstrap FreeKB - Linux Commands - test
Linux Commands - test

Updated:   |  Linux Commands articles

The test command can be used for a variety of tests, such as comparing string "a" to string "b", comparing 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

Let's use the ls (list) command to list the contents of the present working directory. This command should always return exit code 0 (success). The test command in an if statement can be used to display something like "success" when the exit code is 0 or "failed" when the exit code is not zero.

~]# ls
~]# if test $? == 0; then echo "success"; else echo "failed"; fi;
success

 

Issuing a bogus command can be used to see how test and an if statement would return "failed".

~]# bogus
~]# if test $? == 0; then echo "success"; else echo "failed"; fi;
failed

 


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".

~]# test "abc" = "abc"
~]# echo $?
0

 

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

~]# test "abc" = "123"
~]# 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".

~]# test "abc" = "abc" && echo true || echo false
true

 

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

~]# 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".

~]# test "2" -gt "1"
~]# 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.

~]# test -e /home/john.doe
~]# 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.

~]# test -e /home/john.doe/example.file
~]# 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.

~]# test -f /home/john.doe/example.file
~]# 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.

~]# 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.

~]# test -s /home/john.doe/example.file
~]# 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.

~]# 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.

~]# test -r /home/john.doe/example.file
~]# 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.

~]# 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.

~]# test -w /home/john.doe/example.file
~]# 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.

~]# 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.

~]# test -k /home/john.doe/example.file
~]# 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



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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