Originally published on 2010-04-16; fully revised and updated.
This guide converts a legacy shell interview question list into a structured Bash reference for DevOps engineers. It covers essential commands, process control, conditionals and loops, text processing, and safer scripting practices you can use in both interviews and real operations work.

Core shell commands interviewers ask first
These are foundational commands that appear frequently in shell interviews. Knowing both the command and its practical context matters more than memorizing one-liners.
| Interview prompt | Command | What it does |
|---|---|---|
| Find your current shell | echo $SHELL | Prints your login shell path |
| Show current date/time | date | Displays system date and time |
| List logged-in users | who | Shows active logins |
| Show current directory | pwd | Prints working directory |
| Show current username | whoami | Prints effective user |
| Count lines/words/bytes | wc file.txt | Basic file metrics |
| Search text in file | grep "text" file.txt | Matches lines containing text |
| Search recursively in directory | grep -R "text" /path | Recursive search across files |
| Delete a file | rm file.txt | Removes a file |
| Delete recursively | rm -rf /path | Force-removes directory tree (dangerous) |
echo "$SHELL"
date
who
pwd
whoami
wc app.log
grep "ERROR" app.log
grep -R "ERROR" /var/log/myappCode language: PHP (php)
Process and job control essentials
Process control questions often test whether you understand inspection first, termination second. Interviewers usually expect safe handling of PIDs and exit status checks.
| Task | Command | Notes |
|---|---|---|
| List running processes | ps or ps -ef | Output format depends on platform/options |
| Find process by pattern | pgrep -f name | Useful before kill |
| Terminate gracefully | kill -TERM PID | Preferred first signal |
| Force terminate | kill -KILL PID | Use only when graceful stop fails |
| Run in background | command & | Starts job asynchronously |
| Current shell PID | echo $$ | PID of current shell process |
| Last background PID | echo $! | PID of most recent background job |
| Last command status | echo $? | 0 usually indicates success |
| Signal process group | kill 0 | Can affect multiple processes in current process group |
pid="$(pgrep -f my-service)"
if [ -n "$pid" ]; then
kill -TERM "$pid"
sleep 2
ps -p "$pid"
fiCode language: JavaScript (javascript)
Special shell parameters you should remember
| Parameter | Meaning |
|---|---|
$0 | Script name |
$1, $2… | Positional arguments |
$# | Number of positional arguments |
$* | All positional args (single word context) |
$@ | All positional args (preserves argument boundaries when quoted) |
$? | Exit status of last command |
$$ | Current shell PID |
$! | PID of last background command |
Conditionals, tests, and loops in Bash
Interviewers commonly ask for basic control flow syntax and test operators. Be ready to combine numeric, string, and file checks cleanly.
if/elif with numeric, string, and file tests
count=12
file="/tmp/report.txt"
if [ "$count" -gt 10 ] && [ -f "$file" ]; then
echo "Large run and report exists"
elif [ -z "$file" ]; then
echo "No report file specified"
else
echo "Conditions not met"
fiCode language: PHP (php)
for, while, and case patterns
for env in dev stage prod; do
echo "Deploying to $env"
done
i=1
while [ "$i" -le 3 ]; do
echo "Attempt $i"
i=$((i + 1))
done
read -r action
case "$action" in
status) ps -ef ;;
users) who ;;
now) date ;;
*) echo "Invalid option" ;;
esacCode language: PHP (php)
Text processing one-liners frequently asked in interviews
| Task | Command | Notes |
|---|---|---|
| List only empty lines | grep '^$' file.txt | Matches blank lines |
| Remove blank lines | grep -v '^$' file.txt or sed '/^$/d' file.txt | Two common approaches |
| Extract character positions 10-20 | cut -c 10-20 file.txt | Character-based slicing |
| Print line 25 | sed -n '25p' file.txt | Direct and compact |
Replace n with xyz | sed 's/n/xyz/g' file.txt | Global replacement per line |
| List users from passwd file | cut -d: -f1 /etc/passwd | Username field extraction |
grep '^$' file.txt
grep -v '^$' file.txt > no_blanks.txt
sed '/^$/d' file.txt > no_blanks_sed.txt
cut -c 10-20 file.txt
sed -n '25p' file.txt
sed 's/n/xyz/g' file.txt > new_file.txtCode language: JavaScript (javascript)
Legacy reference preserved from the original post: Shell script to read file line by line.
Parsing options with getopts
getopts is a standard way to parse short options like -n 15 -x 20 in shell scripts.
#!/usr/bin/env bash
num=""
xval=""
while getopts "n:x:" opt; do
case "$opt" in
n) num="$OPTARG" ;;
x) xval="$OPTARG" ;;
*) echo "Usage: $0 -n number -x value"; exit 1 ;;
esac
done
echo "n=$num x=$xval"Code language: PHP (php)
Quick conceptual answers from common shell interviews
- What is a named pipe? A FIFO special file used for inter-process communication.
- What does
cmd1 && cmd2mean? Runcmd2only ifcmd1succeeds. - What does
cmd1 || cmd2mean? Runcmd2only ifcmd1fails. - Exported vs non-exported variable: Exported variables are inherited by child processes; non-exported variables remain in the current shell.
- Open a read-only file in vi:
vi -R filename. - Schedule a 4:00 AM daily job: use a cron entry.
crontab -e
0 4 * * * /usr/local/bin/backup.sh
tar -zxvf archive.tar.gz
uname -m
find . -type f -perm 0655 -exec chmod 0755 {} \;Code language: JavaScript (javascript)
Typical script execution workflow
flowchart TD
A[Run script with arguments] --> B[getopts parses options]
B --> C[Validate required inputs]
C --> D{Inputs valid}
D -- no --> E[Print usage and exit 1]
D -- yes --> F[Run main commands]
F --> G[Capture exit code]
G --> H{Exit code is 0}
H -- yes --> I[Report success]
H -- no --> J[Report failure and exit nonzero]
Best practices and common pitfalls
- Prefer safe defaults: inspect with
ps/pgrepbefore usingkill. - Use quoted variables in scripts to avoid word-splitting bugs.
- Treat
rm -rfandkill -KILLas last-resort operations. - Check exit codes (
$?) and return nonzero status on failures. - Use
grep -Rorfinddeliberately instead of broad globs like*in large directories. - Remember that command options and output can vary across Linux/Unix distributions and shells.
If you can explain these commands and patterns clearly, you will handle most shell/Bash interview rounds with confidence.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals