Discover how a simple syntax change can save you from frustrating script errors!
Why [[ ]] extended test in Bash Scripting? - Purpose & Use Cases
Imagine you have a list of files and you want to check if each file exists and is not empty before processing it. Doing this manually means typing multiple commands for each file, checking conditions one by one.
Manually checking each file is slow and easy to forget important checks. You might miss empty files or accidentally process non-existent ones, causing errors or wasted time.
The [[ ]] extended test in bash lets you write clear, concise conditions to check files and other things in one place. It reduces mistakes and speeds up your scripts.
if [ -e file.txt ] && [ -s file.txt ]; then echo 'File is ready'; fi
if [[ -e file.txt && -s file.txt ]]; then echo 'File is ready'; fi
You can write safer, cleaner scripts that handle complex checks easily and avoid common errors.
When backing up important documents, you can use [[ ]] to ensure only existing and non-empty files get copied, preventing backup failures.
Manual checks are slow and error-prone.
[[ ]] lets you combine tests cleanly.
This makes scripts safer and easier to read.
Practice
[[ ]] construct do in a bash script?Solution
Step 1: Understand the purpose of
The[[ ]][[ ]]syntax is used in bash to test conditions such as string comparison, number comparison, and file checks.Step 2: Compare with other options
Options B, C, and D describe other bash features unrelated to[[ ]].Final Answer:
It tests conditions like comparing strings or numbers. -> Option AQuick Check:
[[ ]]= condition test [OK]
- Confusing [[ ]] with function definition syntax
- Thinking [[ ]] creates variables
- Assuming [[ ]] runs commands
var equals "hello" using [[ ]]?Solution
Step 1: Identify correct string comparison operator in [[ ]]
In bash, inside[[ ]], the operator for string equality is==.-eqis for numeric equality.===is invalid.Step 2: Check each option
[[ $var != "hello" ]] checks inequality. [[ $var -eq "hello" ]] uses numeric-eqon string (error). [[ $var === "hello" ]] uses invalid===. [[ $var == "hello" ]] uses==correctly.Final Answer:
[[ $var == "hello" ]] -> Option BQuick Check:
String equality in [[ ]] uses == [OK]
- Using === which is invalid in bash
- Using -eq for string comparison
- Using != when checking for equality
var=5 if [[ $var -gt 3 && $var -lt 10 ]]; then echo "In range" else echo "Out of range" fi
Solution
Step 1: Evaluate numeric conditions inside [[ ]]
The variablevaris 5. The condition checks if 5 is greater than 3 and less than 10, which is true.Step 2: Determine which branch runs
Since the condition is true, the script echoes "In range".Final Answer:
In range -> Option AQuick Check:
5 > 3 and 5 < 10 = true [OK]
- Confusing -gt and -lt operators
- Using single & instead of &&
- Forgetting to quote variables (not critical here)
filename="test.txt" if [[ -f $filename && -r $filename ]] then echo "File exists and readable" fi
Solution
Step 1: Check syntax of [[ ]] condition
The condition starts with[[and has matching closing]]. So brackets are correct.Step 2: Verify other parts
Thethenkeyword is missing after the condition line, which causes a syntax error.Final Answer:
Missing then keyword -> Option DQuick Check:
Every if condition must be followed by then [OK]
- Forgetting the then keyword
- Using single [ with && without escaping
- Ignoring variable quoting warnings
input is either "yes" or "y" (case insensitive) using [[ ]]. Which snippet correctly does this?Solution
Step 1: Understand case-insensitive check
To ignore case, convertinputto lowercase using${input,,}before comparison.Step 2: Check logical OR condition
We want to accept either "yes" or "y", so use||between comparisons.Step 3: Evaluate options
[[ ${input,,} == "yes" || ${input,,} == "y" ]] correctly converts to lowercase and uses OR. [[ $input == "yes" || $input == "y" ]] lacks case conversion. [[ $input =~ ^(yes|y)$ ]] uses regex but is case-sensitive. [[ $input == "yes" && $input == "y" ]] uses AND which requires both to be true, impossible here.Final Answer:
[[ ${input,,} == "yes" || ${input,,} == "y" ]] -> Option CQuick Check:
Use ${var,,} for lowercase and || for OR [OK]
- Not converting input to lowercase
- Using && instead of || for alternatives
- Incorrect regex syntax inside [[ ]]
Ready to prove it?
Pass the test and this pattern moves unknown
