DEV Community

Kazuya Matsumoto
Kazuya Matsumoto

Posted on

What is Linux test command?

Overview

In a word, it is a command to judge conditions.
It can be written as follows.

test condition

In the condition part, write the condition you want to judge and result returns below.

In case of true: 0
In case of false: 1

(It is opposite judgment of general programming language, because of the practice of returning 0 if the UNIX program is normally terminated and 1 if it is abnormally terminated)

Whether a file with a specific file name exists as an example is as follows.

`shell
test -e exist.text; echo $ status # => 0
test -e not_exist.text; echo $ status # => 1
`

Format using []

Apart from the above writing method, the test sentence can be written as follows.


[ conditions ]

With this notation the previous example will be as follows

shell
[ -e exist.text ]; echo $ status # => 0
[ -e not_exist.text ]; echo $ status # => 1

At this time, it is necessary to empty one space in [] as a caveat.

Use with if

In the previous example, we output the result as echo $status, but in reality it is often used with if.
Try writing FizzBuzz with if and test command.

shell
for i in
seq 1 15
do
if [$ (($ i% 3)) - eq 0] && [$ (($ i% 5)) - eq 0]; then
  echo 'FizzBuzz'
elif [$ (($ i% 3)) - eq 0]; then
  echo 'Fizz'
elif [$ (($ i% 5)) - eq 0]; then
  echo 'Buzz'
else
  echo $ i
fi
done

Various condition judgment

The options available with the test command are as follows.

File processing

| Condition | Determination |
|: ----------- | ------------: |
| -d file | True if file is a directory |
| -e file | True if file exists |
| -f file | True if file is a regular file (not a directory, etc.)
| -L file | True if file is a symbolic link |
| -r file | True if file is readable |
| -s file | True if size of file is not 0 |
| -w file | True if file is writable |
| -x file | True if file is executable |

String processing

| Condition | Determination |
|: ----------- | ------------: |
| -z str | True if the length of str is 0 |
| -n str | True if the length of str is greater than 0 |
| str1 == str2 | True if the strings are equal |
| str1 != str2 | True if the strings are not equal |

Numerical processing

| Condition | Determination |
|: ----------- | ------------: |
| int1 -eq int2 | True if int1 and int2 are equal |
| int1 -ne int 2 | True if int1 and int2 are not equal |
| int1 -lt int2 | True if int1 is smaller than int2|
| int1 -le int2 | True if int1 is smaller than equal to int2 |
| int 1 -gt int 2 | True if int 1 is greater than int2 |
| int 1 -ge int 2 | True if int 1 is greater than equal to int2 |

logical condition

| Condition | Determination |
|: ----------- | ------------: |
|! Condition | True if condition is false |
| Condition1 -a Condition2 | True if both Condition1 and Condition2 are true |
| Condition1 -o Condition2 | True if either condition1 or condition2 is true |

Condition determination using && and ||

shell
command1 && command2 # When command1 is executed correctly, execute command2
command1 || command2 # When an error occurs with command1, execute command2

Sometimes you can write an if statement more concisely using the notation

For example

shell
if cat $ 1; then
  echo OK
fi

sh
cat $ 1 && echo OK

sh
if! [$ ((1% 3)) - eq 0]; then
  echo NG
fi

sh
[$ ((1% 3)) - eq 0] || echo NG

You can write like this.

Casually until now
if [hogehoge]
Although I wrote it, I used the test command before I knew it.
It is a useful command which is indispensable when writing a complicated shell.

Top comments (0)