Bash Scripting Interview Questions for DevOps (2026)
Every DevOps engineer needs to write Bash. How do you make your scripts robust and error-proof?
1. What does set -euxo pipefail do?
This is the unofficial Bash strict mode.
- \
-e\: Exit immediately if a command exits with a non-zero status. - \
-u\: Treat unset variables as an error. - \
-x\: Print commands as they are executed. - \
-o pipefail\: The pipeline fails if any command in it fails.
2. How do you find and delete files older than 30 days?
\find /path -type f -mtime +30 -exec rm {} \\;\
Or more efficiently: \find /path -type f -mtime +30 | xargs rm\
3. What is the difference between $# and $@?
\$#\ gives the number of arguments passed to the script.
\$@\ gives all the arguments passed to the script as separate words.
4. How do you read a file line by line?
\\\`bash
while IFS= read -r line; do
echo "$line"
done < input.txt
\\\`
5. What are exit codes in Bash?
Exit codes indicate the success or failure of a command. \0\ means success, any non-zero value (1-255) means failure. The exit code of the last run command is stored in \$?\.
6. How do you replace a string in a file?
Using \sed\: \sed -i 's/old_string/new_string/g' filename\
7. What is process substitution?
Process substitution \<(cmd)\ or \>(cmd)\ allows the output (or input) of a command to appear as a temporary file. Useful for commands that expect files, e.g., \diff <(ls dir1) <(ls dir2)\.
8. What does 2>&1 mean?
It redirects standard error (file descriptor 2) to standard output (file descriptor 1). This combines both stdout and stderr streams.
9. How do you extract the second column of a CSV file?
Using \awk\: \awk -F',' '{print $2}' file.csv\
Or using \cut\: \cut -d',' -f2 file.csv\
10. How do you check if a variable is empty?
\\\`bash
if [ -z "$VAR" ]; then
echo "Variable is empty"
fi
\\\`
11. What is the difference between single and double quotes?
Single quotes (\'\) are literal; variables and commands inside them are NOT evaluated. Double quotes (\"\) allow variable expansion and command substitution (\$VAR\ and \$(cmd)\ are evaluated).
12. How do you run a script in the background?
Append an ampersand \&\ to the command: \./script.sh &\. To keep it running after logging out, use \nohup ./script.sh &\.
13. How do you get the directory of the currently running script?
\DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"\
14. Explain what awk does.
\awk\ is a versatile text processing tool that operates on records (lines) and fields (columns). It is widely used for data extraction, reporting, and manipulating structured text.
15. How do you test if a directory exists?
\\\`bash
if [ -d "/path/to/dir" ]; then
echo "Exists"
fi
\\\`
16. What is the shebang (#!)?
It is the first line of a script (\#!/bin/bash\) indicating which interpreter the OS should use to execute the file.
17. How do you define a function in Bash?
\\\`bash
my_function() {
echo "Argument 1 is $1"
}
my_function "Hello"
\\\`
18. How do you handle default variable values?
Using parameter expansion: \MY_VAR=${MY_VAR:-default_value}\ assigns "default_value" if MY_VAR is unset or empty.
19. How do you parse command line arguments using getopts?
\\\`bash
while getopts "a:b:" opt; do
case $opt in
a) arg_a="$OPTARG" ;;
b) arg_b="$OPTARG" ;;
esac
done
\\\`
20. How do you use cron to schedule a script?
Edit the crontab with \crontab -e\ and add a line like \0 5 * * * /path/to/script.sh\ to run the script every day at 5:00 AM.




