- Using test Command in Bash Scripts
- Using the test command
- String comparison with test Command
- Check if the string is not empty
- Check if the string is empty
- Check if strings are equals
- Check if strings are not equals
- Integer comparison with test command
- Check if numbers are equal
- Check if the numbers are not equal
- Check if a number is equal or greater than the other
- Check if a number is equal or less than the other
- File and directory Operations with test
- Using test command in bash scripts
- Linux test command
- Description
- Syntax
- Expressions
- Examples
- Bash Test Command
- Test command
- Test usage
- Expression
- Implementing test in bash scripts
- Final thoughts
Using test Command in Bash Scripts
Learn to use the test command in bash for testing conditions and making comparisons.
The bash test command is used to check the validity of an expression. It checks whether a command or an expression is true or false. Also, it can be used to check the type and permissions of a file.
If the command or expression is valid, the test command returns a 0 else it returns 1 .
Using the test command
The test command has a basic syntax like this:
When using variables in the test command, use double quotes with the variable’s name.
Let’s use the test command to check whether 10 equals 20 and 10 equals 10:
$ test 10 -eq 20 && echo "true" || echo "false"
- test — test command
- 10 — the first variable
- -eq — operator for comparison
- 20 — second variable
If the given expression is valid, the first command is executed, or else the second command is executed.
💡Instead of using the test command keyword, you can also use the brackets [] . But remember, there is space between the [ mark and the variables to compare:
[ 10 -eq 20 ] && echo "true" || echo "false"
Not only integers; you can also compare strings in bash with the test command. Let me share some examples.
String comparison with test Command
Here are some string comparison examples using the test command.
Check if the string is not empty
The -n flag checks if the string length is non-zero. It returns true if the string is non-empty, else it returns false if the string is empty:
$ [ -n "sam" ] && echo "True" || echo "False"
Check if the string is empty
The -z flag checks whether the string length is zero. If the string length is zero, it returns true, else it returns false:
$ [ -z "sam" ] && echo "True" || echo "False"
Check if strings are equals
The ‘=’ operator checks if string1 equals string2. If the two strings are equal, then it returns 0; if the two strings are not equal, then it returns 1:
In this case, the expression is slightly different, instead of typing true or false, the stdout variable is printed using $? .
Check if strings are not equals
The != operator checks if String1 is not equal to String2. If the two strings are not equal, then it returns 0. If two strings are equal, then it returns 1:
Integer comparison with test command
Let’s make some number comparisons with bash test.
Check if numbers are equal
The -eq operator checks if Integer1 equals Integer2. If integer1 equals integer2, then it returns 0, else it returns 1:
Check if the numbers are not equal
Case 2. The -ne operator checks if integer1 is not equal to integer2. If Integer1 is not equal to integer2, then it returns 0, else it returns 1:
Check if a number is equal or greater than the other
The -ge operator checks that integer1 is greater than or equal to integer2. If integer 1 is greater than or equal to integer 2 it returns 0; if not, then it returns 1.
The -gt operator checks if integer1 is greater than integer2. If yes, then it returns 0. Otherwise, it returns 1:
Check if a number is equal or less than the other
The -le operator checks if the integer1 is less than or equal to integer2. If true it returns 0, else it returns 1:
The -lt operator checks if integer1 is less than integer2. If integer 1 is less than integer2, then it returns 0, else it returns 1:
File and directory Operations with test
The test command can be used with files and directories.
This checks whether a file is executable (by the current user) or not. If a file is executable, then it returns 0, else it returns 1:
[ test -x filename ] && echo executable || echo non-executable
You can use other file permissions like r and w in the same fashion. Other common parameters you can use are:
| Command | Description | |---------|--------------------------| | -e | File/directory exists | | -f | is a file | | -d | is a directory | | -s | File size greater than 0 | | -L | is a link | | -S | is a socket |
Using test command in bash scripts
You have seen the one-liners of the test command so far. You can also use the test condition with the if-else condition in the bash scripts.
Let me share a simple example of comparing two numbers passed as arguments to the shell script:
#!/bin/bash ## Check if the numbers are equal or not read -p "Enter the first number: " num1 read -p "Enter the second number: " num2 if test "$num1" -eq "$num2" then echo "$num1 is equal to $num2" else echo "$num1 is not equal to $num2" fi
You can run the bash script with various numbers:
I hope this tutorial helps you to understand the basics of the test command in the bash.
Linux test command
On Unix-like operating systems, the test command checks file types and compares values.
This page covers the GNU/Linux version of test.
For information about the test command in bash see our bash test command page.
Description
test is used as part of the conditional execution of shell commands.
test exits with the status determined by EXPRESSION. Placing the EXPRESSION between square brackets ([ and ]) is the same as testing the EXPRESSION with test. To see the exit status at the command prompt, echo the value «$?» A value of 0 means the expression evaluated as true, and a value of 1 means the expression evaluated as false.
Syntax
Expressions
Expressions take the following forms:
( EXPRESSION ) | EXPRESSION is true |
! EXPRESSION | EXPRESSION is false |
EXPRESSION1 -a EXPRESSION2 | both EXPRESSION1 and EXPRESSION2 are true |
EXPRESSION1 -o EXPRESSION2 | either EXPRESSION1 or EXPRESSION2 is true |
-n STRING | the length of STRING is nonzero |
STRING | equivalent to -n STRING |
-z STRING | the length of STRING is zero |
STRING1 = STRING2 | the strings are equal |
STRING1 != STRING2 | the strings are not equal |
INTEGER1 -eq INTEGER2 | INTEGER1 is equal to INTEGER2 |
INTEGER1 -ge INTEGER2 | INTEGER1 is greater than or equal to INTEGER2 |
INTEGER1 -gt INTEGER2 | INTEGER1 is greater than INTEGER2 |
INTEGER1 -le INTEGER2 | INTEGER1 is less than or equal to INTEGER2 |
INTEGER1 -lt INTEGER2 | INTEGER1 is less than INTEGER2 |
INTEGER1 -ne INTEGER2 | INTEGER1 is not equal to INTEGER2 |
FILE1 -ef FILE2 | FILE1 and FILE2 have the same device and inode numbers |
FILE1 -nt FILE2 | FILE1 is newer (modification date) than FILE2 |
FILE1 -ot FILE2 | FILE1 is older than FILE2 |
-b FILE | FILE exists and is block special |
-c FILE | FILE exists and is character special |
-d FILE | FILE exists and is a directory |
-e FILE | FILE exists |
-f FILE | FILE exists and is a regular file |
-g FILE | FILE exists and is set-group-ID |
-G FILE | FILE exists and is owned by the effective group ID |
-h FILE | FILE exists and is a symbolic link (same as -L) |
-k FILE | FILE exists and has its sticky bit set |
-L FILE | FILE exists and is a symbolic link (same as -h) |
-O FILE | FILE exists and is owned by the effective user ID |
-p FILE | FILE exists and is a named pipe |
-r FILE | FILE exists and read permission is granted |
-s FILE | FILE exists and has a size greater than zero |
-S FILE | FILE exists and is a socket |
-t FD | file descriptor FD is opened on a terminal |
-u FILE | FILE exists and its set-user-ID bit is set |
-w FILE | FILE exists and write permission is granted |
-x FILE | FILE exists and execute (or search) permission is granted |
Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be escaped (e.g., by backslashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of STRING.
NOTE: your shell may have its own version of test, which usually supersedes the version described here. Please refer to your shell’s documentation for details about the options it supports.
Examples
test 100 -gt 99 && echo "Yes, that's true." || echo "No, that's false."
This command prints the text «Yes, that’s true.» because 100 is greater than 99.
test 100 -lt 99 && echo "Yes." || echo "No."
This command prints the text «No.» because 100 is not less than 99.
This command prints «0» because the expression is true; the two strings are identical.
This command prints «1» because the expression is false; 5 does not equal 6.
Bash Test Command
In bash shell, the test command compares one element against another and returns true or false. In bash scripting, the test command is an integral part of the conditional statements that control logic and program flow.
This guide demonstrates how to use the bash test command.
Test command
The test command takes an EXPRESSION as an argument. After calculating the EXPRESSION, the test returns a value to the bash variable “$?”. If the value is 0, then the expression evaluation was true. If the value is 1, then the expression evaluation was false.
There are two syntaxes for using the test command.
Note that in the case of “[“, there’s a space at both ends of the EXPRESSION.
Test usage
Here’s a short example of how the test command works. We’ll be checking whether 1 equals 2. If true, then the output will be “true”. Otherwise, the output will be “false”.
- test: The test command.
- 1: The first element for comparison.
- -eq: Comparison method (whether values are equal).
- 2: The second element for comparison.
If the test portion is true, then the first echo command will execute. Otherwise, the second echo command will execute.
The same command can be expressed using “[“.
Expression
The expression is what gives the test command its true power. The test can use strings, files, and integers for comparison. Here’s a quick list of all the available test expression formats.
String
In programming, a string is a set of characters that are generally used to represent text. For example, “hello world” in the following echo command is treated as a string.
The test command supports the following string expressions.
- -n : String length is non-zero.
- -z : String length is zero.
- : String value is non-zero (quivalent to “-n ”).
- = : Both string_a and string_b are equal.
- != : The strings string_a and string_b aren’t equal.
Let’s try out these expressions.
The very first example in this guide demonstrates integer comparison. There are more ways to compare integers.
- -eq : Integer_a is equal to integer_b.
- -ne : Integer_a is not equal to integer_b
- -ge : Integer_a is greater than or equal to integer_b.
- -gt : Integer_a is greater than integer_b.
- -le : Integer_a is less than or equal to integer_b.
- -lt : Integer_a is less than integer_b.
Let’s put these expressions into action.
Files can also be part of the expression. Here’s the list of supported file expressions.
- -ef : Both file_a and file_b have similar device and inode number. If it’s true, then it signifies that the files are most likely symlinked. Learn more about Linux symbolic links.
- -nt : In terms of modification date, file_a is newer than file_b.
- -ot : File_a is older than file_b.
The rest of the supported file expressions are related to a single property of a single file.
- -e : File_a exists.
- -f : File_a exists and a regular file.
- -d : File_a exists and is a directory.
- -r : File_a exists with read permissions.
- -w : File_a exists with write permissions.
- -x : File_a exists with execute permissions.
- -s : File_a exists and file size is greater than zero.
- -O : File_a exists and the owner is effective user ID.
- -G : File_a exists and the owner is effective group ID.
- -h : File_a exists and it’s a symbolic link.
- -L : File_a exists and it’s a symbolic link.
- -b : File_a exists. It’s a block-special file.
- -c : File_a exists. It’s a character-special file.
- -S : File_a exists. It’s a socket.
Let’s have a look at some examples.
Implementing test in bash scripts
So far, we’ve demonstrated how to use the test command to determine whether a certain condition is true or false. We can implement this into bash scripts to make useful decisions.
Have a look at the following short script.
Here, the if statement will check whether the condition is true or false. Using the test command, we can easily get the Boolean value.
Run the script with and without root privilege.
It’s a simple if-else statement demonstration. Feel free to check out bash if-else statements for further in-depth applications.
Final thoughts
The test command is simple but powerful. This guide explains and demonstrates various ways of using the test. The complete list of all the supported expressions is available on the man page.