Introduction
The test command and [ ] syntax help check conditions in scripts to decide what to do next.
test CONDITION [ CONDITION ]
test -f filename.txt if [ -f filename.txt ]; then echo "File exists"; fi
if [ "$var" = "hello" ]; then echo "Greeting found"; fi
if test $num -gt 10; then echo "Number $num is greater than 10"; fi
#!/bin/bash filename="myfile.txt" if [ -e "$filename" ]; then echo "File '$filename' exists." else echo "File '$filename' does not exist." fi num=5 if test $num -lt 10; then echo "Number $num is less than 10." else echo "Number $num is 10 or more." fi
[ -f filename ] test check in a bash script?-f test-f option in [ ] checks if the file exists and is a regular file (not a directory or special file).-d checks for directory, -x for executable, and -s for non-empty file, so they don't match.-f means regular file check [OK]var is equal to string hello in bash?= is POSIX compliant for string comparison inside [ ]. Double equals == works in [[ ]] but not always in [ ].var="" if [ -z "$var" ]; then echo "Empty" else echo "Not Empty" fi
-z test-z returns true if the string length is zero (empty string).var is set to empty string "", so [ -z "$var" ] is true and the if branch runs.-z tests empty string [OK]if [ $num -gt 10 ]; then echo "Greater" fi
$num unquoted. If num is empty, the test becomes invalid syntax.data.txt exists and is not empty before processing it in a script. Which condition correctly does this?-f checks if file exists and is regular file; -s checks if file size is greater than zero (not empty).[ ], use -a; or chain separate tests with &&. Using && directly inside [ ] causes syntax error.&& inside single [ ] has syntax error. Chained tests with && are clear and reliable. Options using -o or || apply OR logic, incorrect.Pass the test and this pattern moves unknown