Test command
From Linux Shell Scripting Tutorial - A Beginner's handbook
| ← Bash structured language constructs | Home | if structures to execute code based on a condition → |
The test command is used to check file types and compare values. Test is used in conditional execution. It is used for:
- File attributes comparisons
- Perform string comparisons.
- Arithmetic comparisons.
[edit] test command syntax
test conditionOR
test condition && true-command
OR
test condition || false-command
OR
test condition && true-command || false-command
Type the following command at a shell prompt (is 5 greater than 2? ):
test 5 > 2 && echo "Yes" test 1 > 2 && echo "Yes"
Sample Output:
Yes Yes
Rather than test whether a number is greater than 2, you have used redirection to create an empty file called 2 (see shell redirection). To test for greater than, use the -gt operator (see numeric operator syntax):
test 5 -gt 2 && echo "Yes" test 1 -gt 2 && echo "Yes"
Yes
You need to use the test command while make decision. Try the following examples and note down its output:
test 5 = 5 && echo Yes || echo No test 5 = 15 && echo Yes || echo No test 5 != 10 && echo Yes || echo No test -f /etc/resolv.conf && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found." test -f /etc/resolv1.conf && echo "File /etc/resolv.conf found." || echo "File /etc/resolv.conf not found."
[edit] See also
| ← Bash structured language constructs | Home | if structures to execute code based on a condition → |

