Bash scripting is an essential skill for working with Linux, DevOps, and infrastructure automation. Mastering common bash interview questions is key for any DevOps engineer or Linux admin role. This comprehensive guide covers bash basics to advanced scripting techniques.
Getting Started with Bash Scripting
Bash scripts allow you to automate tasks and utilize the Linux shell‘s powerful text processing capabilities. Here are some bash fundamentals:
Shebang
The shebang at the top of a bash script tells Linux which interpreter to use:
#!/bin/bash
This line is critical – without it, Linux won‘t know to execute the script with bash!
Arguments and Parameters
Special parameters like $1, $2 allow accessing arguments passed when running a script. $# contains the number of arguments and $@ contains all arguments.
Here‘s an example script that prints arguments:
#!/bin/bash
echo "There are $# arguments"
echo "The arguments are: $@"
Functions
Functions help organize bash scripts into reusable blocks:
greeting() {
echo "Hello $1"
}
greeting Jane # Prints "Hello Jane"
Functions can have local scope or access global variables. Return values use the standard Linux exit codes.
Conditional Logic and Looping
Control flow statements allow scripts to make decisions and repeat tasks:
If-Else
The if statement syntax is slightly different in bash compared to other languages:
if [[ condition ]]; then
# statements
elif [[ condition ]]; then
# more statements
else
# default statements
fi
Note the double brackets for test, and the fi to close.
Case Statements
Case statements are useful alternatives to chained if-elif blocks:
case $variable in
val1)
# Statements
;;
val2)
# More statements
;;
*)
# Default
;;
esac
Loops
Common loops include for, while, and until loops for repeating blocks of code. Useful techniques include reading files line-by-line and using control operators like break and continue.
Here is an example using a C-style for loop:
for (( i=1; i <= 10; i++ )); do
echo $i
done
And reading a file:
while read line; do
echo $line
done < file.txt
Best Practices
Quote Variables
Always quote variables to avoid whitespace errors:
name="John"
greeting="Hello $name" # Error prone
greeting="Hello $name" # Safer
Check Return Codes
Check return codes of commands to catch errors:
if grep -q ‘foo‘ file.txt; then
echo "Found"
else
echo "Not Found"
fi
Avoid Globbing Issues
Use quotes when globbing may cause unintended results:
mv *.txt /backup # Potentially dangerous
mv ‘*.txt‘ /backup # Safer
Input/Output
Bash provides many ways to consume input and produce output:
Reading Input
- $REPLY variable from a read statement
- Command substitution with $( )
- Reading from files with loops
- Piping data between commands
Writing Output
- Echo to standard out
- Redirect with >, >> to files
- Send to stderr with echo >&2
- Control stdout/stderr with exec
Here is an example that reads input and writes output:
#!/bin/bash
read -p "Enter your name: " name
greeting="Hello $name!"
echo $greeting
echo $greeting > output.txt
Debugging and Defensive Coding
Robust defensive coding habits prevent bugs and errors:
Logging and Debug Print Statements
Liberal use of echo statements is an easy way to add logging:
echo "Got to first loop"
((count++))
# Additional logic...
echo "Finished first loop"
Checking Variables
Defensive coding checks if variables are set before usage:
if [ -z "$input"]; then
echo "Missing required variable"
exit 1
fi
# Proceed with $input
Handling Errors
Check return codes from commands and implement error handling logic:
tar cf archive.tar files || {
echo "Taring failed, exiting"
exit 1
}
This covers the most important bash scripting interview questions – best of luck!


