You need to use the test command to perform various numeric comparison using the following operators:
- INTEGER1 -eq INTEGER2 – INTEGER1 is equal to INTEGER2 [donotprint]
- 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
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | None |
Time | 1m |
Say hello to test utility
The test command evaluates the expression and, if it evaluates to true, returns a zero (true) exit status; otherwise it returns 1 (false). The syntax is:
test condition test condition && do_something || do_nothing_due_to_false [ condition ] && do_something || do_nothing_due_to_false
The syntax with if command is as follows:
if [ condition ]; then do_run_this_due_to_true_condition else do_run_this_due_to_false_condition fi
Example: Comparing numbers in bash
Find out if 5 greater than 10, enter (type command at terminal):
x=5 y=10 [ $x -gt $y ] echo $?
Sample outputs:
1
In a bash shell non-zero output means false result i.e. $x is not greater than $y. Try the following example (type command at terminal):
x=51 y=10 [ $x -gt $y ] echo $?
0
A zero value means true result i.e $x is greater than $y. Let us make output more readable using the following syntax:
[ condition ] && true-command || false-command
Find out if 5 greater than 10, enter (type command at terminal):
x=5 y=10 [ $x -gt $y ] && echo "Yes \$x > \$y " || echo "No \$x is not > \$y"
No $x is not > $y
You can use if statement as follows:
#!/bin/bash read -p "Enter a number (must be greater than 20) : " n if test $n -gt 20 then echo "$n is greater than 20." else echo "You are not following my instructions." fi
OR
#!/bin/bash read -p "Enter a number (must be greater than 20) : " n if [ $n -gt 20 ]; then echo "$n is greater than 20." else echo "You are not following my instructions." fi
Run it as follows:
./script.sh Enter a number (must be greater than 20) : 22 22 is greater than 20. ./script.sh Enter a number (must be greater than 20) : 8 You are not following my instructions.
Arithmetic tests options
You can see a list of all supported options it by typing the following command:
$ help test
Sample outputs:
File operators:
-a FILE True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its `sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read.
FILE1 -nt FILE2 True if file1 is newer than file2 (according to
modification date).
FILE1 -ot FILE2 True if file1 is older than file2.
FILE1 -ef FILE2 True if file1 is a hard link to file2.
String operators:
-z STRING True if string is empty.
-n STRING
STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal.
STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically.
Other operators:
-o OPTION True if the shell option OPTION is enabled.
! EXPR True if expr is false.
EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
Arithmetic binary operators return true if ARG1 is equal, not-equal,
less-than, less-than-or-equal, greater-than, or greater-than-or-equal
than ARG2.
See also
- Chapter 4: Conditionals Execution (Decision Making)
- See bash(1) man page for more info.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 2 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
This was a very useful article, thank you!
Obrigado pelo artigo, me ajudou bastante.
From Brazil, Thank you.