Shell scripting is a core skill for Linux administrators, DevOps engineers and security analysts. These shell scripting interview questions focus on Bash, the most common shell, and cover the basics, special variables, control flow, quoting, automation and debugging. Try each example in a terminal as you read.

Basics
1. What is a shell script?
A shell script is a text file containing commands that the shell runs in order. It automates repetitive tasks, such as backups, log cleanup or user creation.
2. Why does a script start with #!/bin/bash?
That first line is the shebang. It tells the system which interpreter should run the script. For portability, many scripts use #!/usr/bin/env bash.
3. How do you run a script?
Make it executable with chmod +x script.sh, then run ./script.sh. Alternatively, run bash script.sh without changing permissions.
4. Name some advantages and drawbacks of shell scripting.
Advantages: it is available on every Linux system, great for gluing commands together and quick to write. Drawbacks: it is slower than compiled languages, error handling is limited and large scripts become hard to maintain. For complex logic, Python is often a better choice.
Variables and parameters
5. How do you define and use a variable?
name="Alice"
echo "Hello, $name"There must be no spaces around =.
6. Explain the special parameters.
| Parameter | Meaning |
|---|---|
$0 | Script name |
$1, $2… | Positional arguments |
$# | Number of arguments |
$@ | All arguments, as separate words |
$? | Exit status of the last command |
$$ | Process ID of the current shell |
$! | Process ID of the last background job |
7. What is an exit status?
Every command returns a number when it ends. Zero means success, and any other value means failure. Check it with $?, or use && and || to chain commands based on the result.
Control flow
8. Write an if statement that checks whether a file exists.
if [[ -f /etc/passwd ]]; then
echo "File exists"
else
echo "Missing"
fiCommon tests include -f (file), -d (directory), -z (empty string) and -eq (numbers equal).
9. Show a for loop and a while loop.
for host in web1 web2 db1; do
ping -c1 "$host" >/dev/null && echo "$host up"
done
while read -r line; do
echo "User: $line"
done < users.txt10. When would you use a case statement?
Use case to match one value against several patterns, such as handling start, stop and restart options in a service script.
Quoting, input and output
11. Single quotes vs double quotes?
Double quotes allow variable and command expansion, while single quotes keep text exactly as written. As a rule, always quote variables, such as "$file", to handle spaces safely.
12. How do you capture command output?
Use command substitution: today=$(date +%F).
13. Explain redirection.
>writes output to a file, replacing it.>>appends to a file.2>redirects errors.&>redirects both output and errors.|sends one command’s output to another.
Automation and safety
14. How do you schedule a script?
Use cron. For example, 0 2 * * * /opt/scripts/backup.sh runs the script every day at 02:00. Alternatively, use systemd timers on modern systems.
15. How do you make scripts safer?
Start with set -euo pipefail. It stops on errors, treats unset variables as errors and catches failures inside pipes. Also, validate input and avoid running as root unless needed.
16. How do you debug a script?
Run bash -x script.sh to print each command as it runs. In addition, a linter such as ShellCheck finds common bugs before you run the script.
17. How do you pass and check arguments?
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <directory>" >&2
exit 1
fiFor more Linux practice, see our Linux interview questions and the Linux quiz.
Keep preparing
Practise each answer out loud, and run the commands in a lab rather than memorising them. Interviewers often ask follow-up questions, so make sure you understand why each answer is true.
Sources
Preparing for an interview? Browse more questions in our interview preparation hub. For businesses, we also offer security services such as VAPT, server hardening and ISO 27001 readiness.
