Concept Flow - [[ ]] extended test
Start
Evaluate [[ expression
Is expression true?
No→Execute else or skip
Yes
Execute then block
End
The [[ ]] extended test evaluates a condition. If true, it runs the then block; if false, it runs else or skips.
if [[ -f /etc/passwd && -r /etc/passwd ]]; then echo "File exists and is readable" else echo "File missing or not readable" fi
| Step | Expression Evaluated | Result | Branch Taken | Output |
|---|---|---|---|---|
| 1 | -f /etc/passwd | true | Continue | |
| 2 | -r /etc/passwd | true | Continue | |
| 3 | -f /etc/passwd && -r /etc/passwd | true | Then block | File exists and is readable |
| 4 | End of if | N/A | Exit |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| -f /etc/passwd | N/A | true | true | true |
| -r /etc/passwd | N/A | N/A | true | true |
| Combined expression | N/A | N/A | N/A | true |
[[ ]] extended test syntax: if [[ condition ]]; then commands fi Supports complex tests with &&, ||, pattern matching. Returns true or false to control flow. Use for safer, more powerful condition checks.
[[ ]] construct do in a bash script?[[ ]][[ ]] syntax is used in bash to test conditions such as string comparison, number comparison, and file checks.[[ ]].[[ ]] = condition test [OK]var equals "hello" using [[ ]]?[[ ]], the operator for string equality is ==. -eq is for numeric equality. === is invalid.-eq on string (error). [[ $var === "hello" ]] uses invalid ===. [[ $var == "hello" ]] uses == correctly.var=5 if [[ $var -gt 3 && $var -lt 10 ]]; then echo "In range" else echo "Out of range" fi
var is 5. The condition checks if 5 is greater than 3 and less than 10, which is true.filename="test.txt" if [[ -f $filename && -r $filename ]] then echo "File exists and readable" fi
[[ and has matching closing ]]. So brackets are correct.then keyword is missing after the condition line, which causes a syntax error.input is either "yes" or "y" (case insensitive) using [[ ]]. Which snippet correctly does this?input to lowercase using ${input,,} before comparison.|| between comparisons.Pass the test and this pattern moves unknown