KandZ – Tuts

We like to help…!

Linux CLI 60 🐧 test command in shell scripts

a - test command part 1
test command is used to evaluate conditional expressions
it returns an exit status based on whether the expression evaluates to true or false.
It can be used in if statements, loops, and case statements.
test command has a wide range of expressions:
file, string and integer expressions
Common uses:
Numeric comparisons: -eq, -ne, -lt, -le, -gt, -ge
String comparisons: = (equal), != (not equal)

b - test command part 2
File tests: -e: True if the file exists.
-f: True if the file is a regular file.
-d: True if the directory exists.
-r: True if the file is readable.
-w: True if the file is writable.
-x: True if the file is executable.
Logical Operators: -a: Logical AND (deprecated, use && instead).
-o: Logical OR (deprecated, use || instead).
!: Logical NOT.
String Length:-z: True if the string is null (zero length).
-n: True if the string is not null.
The square brackets [ ] are often used as a more readable alternative to the test command.

Leave a Reply