What if your script could ask itself questions and never get confused?
Why test command and [ ] syntax in Bash Scripting? - Purpose & Use Cases
Imagine you want to check if a file exists before using it in your script. You open the file manager, look for the file, then decide what to do next. Doing this for many files or conditions means lots of clicking and guessing.
Manually checking conditions like file existence or comparing values is slow and easy to forget. It's like trying to remember every step without a checklist. Mistakes happen, and your script might break or behave unexpectedly.
The test command and its shortcut [ ] let you quickly check conditions inside your script. They act like smart questions your script asks itself to decide what to do next, making your script reliable and clear.
if file exists then
do something
fiif [ -f filename ]; then
do something
fiIt lets your script make smart decisions automatically, like checking files or comparing values, so you don't have to do it by hand.
Before running a backup, your script checks if the backup folder exists using [ -d /backup ]. If it's missing, the script creates it, preventing errors and saving you from manual fixes.
Manual checks are slow and error-prone.
test and [ ] let scripts check conditions easily.
This makes scripts smarter and more reliable.
Practice
[ -f filename ] test check in a bash script?Solution
Step 1: Understand the
The-ftest-foption in[ ]checks if the file exists and is a regular file (not a directory or special file).Step 2: Compare with other options
-dchecks for directory,-xfor executable, and-sfor non-empty file, so they don't match.Final Answer:
If filename exists and is a regular file -> Option DQuick Check:
-fmeans regular file check [OK]
- Confusing -f with -d for directories
- Forgetting spaces inside [ ]
- Using filename without quotes causing errors
var is equal to string hello in bash?Solution
Step 1: Check quoting rules
Variables should be quoted to avoid errors if empty or contain spaces, so "$var" is correct.Step 2: Check operator correctness
Single equals=is POSIX compliant for string comparison inside [ ]. Double equals==works in [[ ]] but not always in [ ].Final Answer:
[ "$var" = "hello" ] -> Option CQuick Check:
Quote variables and use = inside [ ] [OK]
- Not quoting variables causing syntax errors
- Using == inside [ ] which may fail
- Missing spaces around brackets
var="" if [ -z "$var" ]; then echo "Empty" else echo "Not Empty" fi
Solution
Step 1: Understand the
-ztest-zreturns true if the string length is zero (empty string).Step 2: Check variable value
Variablevaris set to empty string "", so[ -z "$var" ]is true and the if branch runs.Final Answer:
Empty -> Option AQuick Check:
-ztests empty string [OK]
- Not quoting variable causing errors
- Confusing -z with -n
- Expecting else branch to run
if [ $num -gt 10 ]; then echo "Greater" fi
Solution
Step 1: Check variable usage in test
When using variables in [ ], they should be quoted to avoid errors if empty or unset.Step 2: Analyze the given code
The code uses$numunquoted. Ifnumis empty, the test becomes invalid syntax.Final Answer:
Variable $num should be quoted -> Option BQuick Check:
Always quote variables in [ ] to avoid syntax errors [OK]
- Not quoting variables causing syntax errors
- Confusing numeric and string operators
- Forgetting spaces around brackets
data.txt exists and is not empty before processing it in a script. Which condition correctly does this?Solution
Step 1: Understand the tests
-fchecks if file exists and is regular file;-schecks if file size is greater than zero (not empty).Step 2: Choose correct logical operator
To require both conditions, use logical AND. Inside a single[ ], use-a; or chain separate tests with&&. Using&&directly inside[ ]causes syntax error.Step 3: Analyze options
The option with&&inside single[ ]has syntax error. Chained tests with&&are clear and reliable. Options using-oor||apply OR logic, incorrect.Final Answer:
[ -f data.txt ] && [ -s data.txt ] -> Option AQuick Check:
Use && to combine separate [ ] tests [OK]
- Using -o or || which means OR, not AND
- Using && inside a single [ ] (syntax error)
- Not checking file size with -s
Ready to prove it?
Pass the test and this pattern moves unknown
