Bash Programming Cheat Sheet Page 2

ADVERTISEMENT

Bash Programming Cheat Sheet
Page 2 of 4
Logic and comparisons
A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the
entrance/exit criteria for a loop.
test expression
Or
[ expression ]
Numeric Comparisons
int1 -eq int2 Returns True if int1 is equal to int2.
int1 -ge int2 Returns True if int1 is greater than or equal to int2.
int1 -gt int2 Returns True if int1 is greater than int2.
int1 -le int2 Returns True if int1 is less than or equal to int2
int1 -lt int2 Returns True if int1 is less than int2
int1 -ne int2 Returns True if int1 is not equal to int2
String Comparisons
str1 = str2 Returns True if str1 is identical to str2.
str1 != str2 Returns True if str1 is not identical to str2.
str
Returns True if str is not null.
-n str
Returns True if the length of str is greater than zero.
-z str
Returns True if the length of str is equal to zero. (zero is different than null)
File Comparisons
-d filename Returns True if file, filename is a directory.
-f filename Returns True if file, filename is an ordinary file.
-r filename Returns True if file, filename can be read by the process.
-s filename Returns True if file, filename has a nonzero length.
-w filename Returns True if file, filename can be written by the process.
-x filename Returns True if file, filename is executable.
Expression Comparisons
!expression
Returns true if expression is not true
expr1 -a expr2
Returns True if expr1 and expr2 are true. ( && , and )
expr1 -o expr2
Returns True if expr1 or expr2 is true. ( ||, or )
18/3/2553

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4