- Use: Test conditions (same as
testcommand). - Example:
if [ -f /etc/passwd ]; then echo "File exists" fi
- DevOps use case: Checking if config/log files exist before deploying or restarting services.
-
Use: Advanced test syntax (Bash/Ksh only). Safer than
[ ]. -
Supports regex
=~, logical operators&& ||, and no word-splitting issues. -
Example:
if [[ $USER =~ ^dev.* ]]; then echo "Dev user detected" fi
[!NOTE]
in if block Use [ ] for portable POSIX scripts (works in sh in -gt like condition) in if block Use[[ ]] for Bash/Ksh scripts (safer, more features in both manner like <= and -le). In if block Use (( )) for arithmetic. -
Production use case: Validating usernames, branch names, or environment variables in CI/CD pipelines.
- Use:
- Variable expansion →
${var}avoids ambiguity. - Brace expansion → Generate strings/sequences.
- Variable expansion →
- Examples:
echo "Hello ${USER}!" echo file{1..3}.log # file1.log file2.log file3.log
- Production use case: Creating multiple log files, batch renaming, or looping over ranges in automation scripts.
- Use:
- Subshell → Commands inside run in a new shell.
- Arrays → Define Bash arrays.
- Examples:
(cd /tmp && ls) # runs in subshell, doesn’t affect current dir arr=(one two three) echo ${arr[1]} # two
- Production use case:
- Running isolated commands without changing current environment.
- Managing arrays of servers or configs in deployment scripts.
- Use: Arithmetic evaluation.
- Example:
count=0 ((count++)) echo $count # 1
- Production use case: Counters in retry loops, monitoring scripts, or resource checks.
- Condition check: “How would you verify if a log file exists before parsing it?” →
[ -f logfile ]. - Regex validation: “Check if a branch name starts with
feature/.” →[[ $branch =~ ^feature/ ]]. - Batch file creation: “Generate 10 numbered config files.” →
touch config{1..10}.yaml. - Subshell isolation: “Run a command in
/tmpwithout leaving current directory.” →(cd /tmp && ls). - Retry logic: “Increment a counter until a service responds.” →
((count++)).
Here’s a cheat sheet of 10 real-world DevOps/production interview questions focused on bracket usage in Linux shell scripting, with short answers:
-
File existence check
Q: How do you check if/etc/passwdexists?
A:if [ -f /etc/passwd ]; then echo "exists"; fi -
String pattern match
Q: Validate if$branchstarts withfeature/.
A:if [[ $branch =~ ^feature/ ]]; then echo "valid"; fi -
Multiple file creation
Q: Create 10 numbered log files in one command.
A:touch log{1..10}.txt -
Variable expansion safety
Q: Print$HOMEfollowed by_backup.
A:echo "${HOME}_backup" -
Subshell isolation
Q: List files in/tmpwithout leaving current directory.
A:(cd /tmp && ls) -
Array usage
Q: Store three server names and print the second.
A:servers=(app1 app2 app3) echo ${servers[1]}
-
Arithmetic increment
Q: Increase a retry counter by 1.
A:((retry++)) -
Logical AND condition
Q: Check if$userisadminAND$envisprod.
A:if [[ $user == "admin" && $env == "prod" ]]; then echo "ok"; fi -
Brace expansion with ranges
Q: Generate filenamesconfigA,configB,configC.
A:echo config{A..C} -
Numeric comparison
Q: Check if$countis greater than 5.
A:if (( count > 5 )); then echo "high"; fi
Here’s a mini practice test with 5 timed-style questions on bracket usage in Linux shell scripting, designed like real DevOps interview scenarios. Try answering them quickly as if you’re in an interview:
Here’s a 20-question full mock test on Linux shell scripting bracket usage, styled like real DevOps/production interview scenarios. Each question is practical, concise, and tests logic you’d actually use in CI/CD, monitoring, or support scripts.
- Check if
/etc/hostsexists before appending a new entry. - Verify if
/var/log/app.logis non-empty. - Ensure
$USERis not equal toroot. - Test if
$PORTis greater than 1024. - Confirm that
$envis eitherdevortest.
- Validate if a branch name starts with
feature/. - Ensure a filename ends with
.yaml. - Check if
$emailcontains@company.com. - Verify if
$versionmatches semantic versioning (X.Y.Z). - Confirm
$hostnamestarts withprod-.
- Generate 10 numbered log files (
log1.log…log10.log). - Create configs named
configA,configB,configC. - Expand
{dev,test,prod}into environment names. - Produce a sequence of numbers from 5 to 15.
- Generate filenames
server1.conftoserver3.conf.
- Run
lsinside/tmpwithout leaving current directory. - Store three IPs in an array and print the second.
- Execute
(cd /var && ls)and confirm your current directory remains unchanged. - Use an array to loop over servers
app1,app2,app3. - Run two commands in a subshell: change directory and list files.
[ -f /etc/hosts ][ -s /var/log/app.log ][ "$USER" != "root" ](( PORT > 1024 ))[[ $env == "dev" || $env == "test" ]][[ $branch =~ ^feature/ ]][[ $file == *.yaml ]][[ $email == *@company.com ]][[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]][[ $hostname =~ ^prod- ]]touch log{1..10}.logtouch config{A..C}echo {dev,test,prod}echo {5..15}echo server{1..3}.conf(cd /tmp && ls)ips=(10.0.0.1 10.0.0.2 10.0.0.3); echo ${ips[1]}(cd /var && ls); pwdservers=(app1 app2 app3); for s in "${servers[@]}"; do echo $s; done(cd /etc && ls)
Example 1
read N
sum=0
count=$N
while(( $N>= 1))
do
read x
sum=$(( sum + x ))
#arr+=($x)
(( N-- ))
done
#avg=$(( sum / count )) not working
printf "%0.3f" "$( echo "scale=4; $sum / $count " |bc)"