Hey there, tech fam! If you’re gearin’ up for a Linux interview and got “shell scripting” on your radar, you’re in the right spot. I’m gonna walk ya through everything you need to know about shell scripting interview questions—from the basics that’ll get you in the door to the hardcore stuff that’ll land you a fat paycheck. Whether you’re aimin’ for a sysadmin role or dreamin’ of being a DevOps wizard, mastering shell scripting is your golden ticket. So, let’s dive in and get you prepped to crush that technical screen!
Why Shell Scripting Matters (And Why It’s Worth Big Bucks)
Before we get into the nitty-gritty, let’s chat about why shell scripting is such a big deal. In the US job market, especially in 2026, knowing how to whip up a Bash script ain’t just a “nice-to-have”—it’s a straight-up money maker. We’re talkin’ roles like Linux Sysadmin ($85K–$110K), DevOps Engineer ($110K–$145K), or even Site Reliability Engineer (SRE) pullin’ in $130K–$175K. Why? ‘Cause shell scripting is all about automation—tellin’ your computer to stop buggin’ ya with repetitive tasks. And companies, from startups in NYC to big dogs like AWS, are payin’ top dollar for folks who can save time and prevent screw-ups.
Here’s the real kicker: shell scripting ain’t programming in the fancy coder sense. It’s just takin’ commands you already know and puttin’ ‘em in a file to run on repeat. If you’ve messed around in a Linux terminal, you’re halfway there. Interviewers wanna see if you can think practical, solve real-world probs, and not just parrot syntax. So, let’s break down the kinda questions you’ll face and how to nail ‘em.
Beginner-Level Shell Scripting Questions: Start Strong
Every interview kicks off with the basics Mess these up, and it’s game over before ya even start Nail ‘em with confidence, and you’re signalin’ you ain’t messin’ around. Here’s what to expect in the first five minutes.
1. What’s the Deal with the Shebang Line?
- Question: What’s a shebang line, and why’s it in every script?
- Answer: Alright, the shebang line is that
#!/bin/bashthing at the top of every shell script. It’s like tellin’ your system, “Hey, use Bash to run this bad boy.” Without it, the OS might guess wrong and botch the whole thing. It’s a must for makin’ sure your script runs right, no matter what shell someone’s logged into. - Code Example:
bash
#!/bin/bashecho "Yo, I’m runnin’ in Bash!" - Tip: Always mention it ensures “portability.” Interviewers eat that up.
2. How Do Ya Make a Script Executable?
- Question: How do you make a shell script executable and run it?
- Answer: Easy peasy. First, ya gotta give it permission with
chmod +x myscript.sh. Then, run it with./myscript.shor straight-upbash myscript.sh. I usually go withbashin pipelines to dodge permission headaches. - Code Example:
bash
chmod +x myscript.sh./myscript.sh - Tip: Forgetting
chmodis a rookie move. Don’t be that guy.
3. What Are Special Variables in Bash?
- Question: Name some special variables in Bash and what they do.
- Answer: These are built-in goodies Bash gives ya to track what’s goin’ on. Here’s five I always got in my back pocket:
$0: Name of the script itself.$1: First argument ya pass to the script.$#: How many arguments got passed.$?: Exit code of the last command (0 means all good).$$: PID of the script runnin’ right now.
- Why It Matters: Droppin’
$?and sayin’ ya use it for error checkin’ shows you think about real-world stuff, not just theory. - Tip: Explain how ya use one, like
$?to catch fails. That’s a pro move.
Pro Hint When answerin’ these beginner questions don’t just spit out facts. Toss in a quick “why”—like “I use this to make sure backups don’t flop.” It shows you’ve been in the trenches.
Intermediate Shell Scripting Questions: Step Up Your Game
Alright, now we’re gettin’ into the meaty stuff. These questions split the $85K crowd from the $110K–$130K earners. Interviewers wanna see ya can handle real-world automation with loops, conditions, and solid error catchin’.
4. Automate a Cleanup Task with a Script
- Question: Write a script for a daily cleanup job and explain it.
- Answer: This is your chance to shine. Shell scripting is all about makin’ life easier, so show ‘em a script that does boring stuff for ya. Here’s one I’d use for log cleanup:
bash
#!/bin/bash# cleanup.sh - Daily log cleanup for lazy admins like meLOG_DIR="/var/log/myapp"BACKUP_DIR="/backup/logs"DAYS_TO_KEEP=7# Make backup spot if it ain’t theremkdir -p "$BACKUP_DIR"# Zip logs older than 7 daysfind "$LOG_DIR" -name "*.log" -mtime +$DAYS_TO_KEEP -exec gzip {} ;# Move zipped stuff to backupmv "$LOG_DIR"/*.gz "$BACKUP_DIR/" 2>/dev/null# Tell me it’s doneecho "[$(date)] Cleanup done. Logs over ${DAYS_TO_KEEP} days archived." - Explanation: I quote variables with
""to handle weird file names with spaces.mkdir -pstops errors if the folder’s already there. And timestampin’ the echo helps track when it ran—super handy for audits. - Tip: Mention cron schedulin’ (like
0 2 * * * /path/to/script.sh) to run this daily. Shows ya think ahead.
5. How Do Ya Handle Errors in Scripts?
- Question: How do you deal with errors in Bash? What’s
set -Eeuo pipefail? - Answer: This is huge. Most scripts just die quietly if somethin’ goes wrong. Not cool in production. I start every serious script with
set -Eeuo pipefail. Here’s what it does:-E: Makes sure error traps work in functions.-e: Stops the script if any command flops.-u: Freaks out if ya use an unset variable.-o pipefail: Catches fails in piped commands, not just the last one.
Here’s a quick look:
bash#!/bin/bashset -Eeuo pipefailtrap 'echo "Whoops! Failed at line $LINENO" >&2' ERRdeploy() { echo "Pushin’ to AWS..." aws s3 sync ./dist s3://my-bucket --delete echo "All done."}deploy - Why It Matters: Without this, a failed command could keep rollin’ and trash your data. Not a good look in a regulated gig.
- Tip: Always say ya use this for “production-grade” scripts. Sounds fancy, works great.
6. Show Me Loops—For and While
- Question: Write a
forandwhileloop. When do ya use each? - Answer: Loops are your bread and butter for repeat tasks. Here’s both:
bash
# For loop - When ya got a set listfor server in web01 web02 web03; do echo "Checkin’ $server..." ssh "$server" "systemctl status nginx"done# While loop - When ya waitin’ on somethin’RETRIES=0while ! ping -c1 8.8.8.8 &>/dev/null; do echo "Network’s down, waitin’... (try $RETRIES)" RETRIES=$((RETRIES + 1)) sleep 5 [ $RETRIES -ge 10 ] && { echo "I’m done waitin’."; exit 1; }doneecho "Network’s back!" - When to Use: I go
forwhen I got a fixed list—servers, files, whatever.whileis for retryin’ stuff, like waitin’ for a connection. - Tip: Mention real uses, like
whilefor AWS Lambda retries. Shows ya know the game.
Pro Hint These mid-level questions test if ya can automate without breakin’ stuff Always tie your answer to a real scenario—I’ve seen folks get props for sayin’ “this saved my butt on a deploy”
Advanced Shell Scripting Questions: Prove You’re Worth $130K+
Now we’re in big-league territory. These are for SRE or senior DevOps roles where ya gotta show scripts that don’t just work—they survive disasters, debug easy, and play nice with teams.
7. Write a Deployment Script with Rollback
- Question: Show me a deployment script with rollback if it fails.
- Answer: This is where ya prove you’ve shipped real code. A deploy without rollback is a lawsuit waitin’ to happen in regulated shops. Check this:
bash
#!/bin/bashset -euo pipefailAPP_DIR="/opt/myapp"BACKUP_DIR="/opt/myapp-backup-$(date +%Y%m%d-%H%M%S)"NEW_BUILD="./dist"rollback() { echo "[ROLLBACK] Deploy tanked. Restorin’ backup..." rm -rf "$APP_DIR" cp -r "$BACKUP_DIR" "$APP_DIR" systemctl restart myapp echo "[ROLLBACK] We’re good. Service back." exit 1}trap rollback ERR# Backup firstecho "Backin’ up current stuff..."cp -r "$APP_DIR" "$BACKUP_DIR"# Deploy new junkecho "Droppin’ new build..."rsync -av --delete "$NEW_BUILD/" "$APP_DIR/"# Restart and checksystemctl restart myappsleep 3systemctl is-active --quiet myapp || { echo "Service ain’t startin’"; exit 1; }echo "Deploy went smooth!" - Why It Matters:
trap ERRcatches any hiccup and rolls back automatic-like. This is gold for CI/CD pipelines. - Tip: Say ya use this for “zero-downtime deploys.” Sounds pro as heck.
8. Cron Jobs and Debuggin’ ‘Em
- Question: How do ya set up a cron job and debug one that ain’t runnin’?
- Answer: Cron’s how ya schedule stuff to run without babysittin’. But when it fails, it’s a silent killer. Here’s the setup:
bash
# Edit cron for current usercrontab -e# Runs daily at 2 AM0 2 * * * /opt/scripts/cleanup.sh >> /var/log/cleanup.log 2>&1 - Debuggin’: If it ain’t runnin’, I check:
- Logs with
grep CRON /var/log/syslog | tail -20. - Run it manual with
env -i HOME=/root PATH=/usr/bin:/bin bash /opt/scripts/cleanup.sh—cron’s got no env vars. - Peek at output in
/var/log/cleanup.log.
- Logs with
- Why It Matters: Cron fails often ‘cause of PATH issues. Usin’ absolute paths and loggin’ everything saves ya.
- Tip: Drop
env -iin your answer. Senior folks know this trick; beginners don’t.
Pro Hint: For advanced stuff, talk while ya write in live codin’ rounds. Say stuff like, “I’m usin’ set -euo pipefail ‘cause I don’t trust silent fails.” Interviewers dig that thought process.
Live Coding Challenges: Be Ready to Type
Top tech joints, especially in places like Seattle or NYC, love throwin’ ya into a shared terminal for live codin’. Here’s three prompts I’ve seen a bunch. Practice ‘til ya can do ‘em blind.
Prompt 1: Disk Usage Alert Script
- Task: Write a script to check disk usage and alert if over 80%.
- Answer:
bash
#!/bin/bashset -euo pipefailTHRESHOLD=80df -h | awk 'NR>1 {gsub(/%/,"",$5); if($5+0 >= '$THRESHOLD') print "ALERT: "$6" is "$5"% full"}' - Talk Through: I’m usin’
df -hfor usage, pipin’ toawkto yank the % and compare. Skippin’ the header withNR>1keeps it clean.
Prompt 2: Service Watchdog Script
- Task: Script to check if a service is runnin’ and restart if not.
- Answer:
bash
#!/bin/bashSERVICE="nginx"if systemctl is-active --quiet "$SERVICE"; then echo "$SERVICE is up."else echo "$SERVICE is down. Restartin’..." systemctl restart "$SERVICE" sleep 2 if systemctl is-active --quiet "$SERVICE"; then echo "$SERVICE back online." else echo "CRITICAL: $SERVICE won’t restart. Callin’ on-call." >&2 exit 1 fifi - Tip: Add cron to run every 5 mins with
*/5 * * * * /path/to/script.sh >> /log 2>&1.
Prompt 3: Backup Script for Production
- Task: Write an enhanced backup script.
- Answer:
bash
#!/bin/bashset -euo pipefailBACKUP_DIR=~/backups/$(date +%Y-%m-%d)SOURCES=("~/projects" "~/Documents")LOG="/var/log/backup.log"mkdir -p "$BACKUP_DIR"for SRC in "${SOURCES[@]}"; do SRC_EXPANDED=$(eval echo "$SRC") cp -r "$SRC_EXPANDED" "$BACKUP_DIR/" && echo "[$(date)] Backed up $SRC" >> "$LOG" || echo "[$(date)] FAILED: $SRC" >> "$LOG"doneecho "Backup done to $BACKUP_DIR" - Talk Through: Arrays for sources, loggin’ every step, and not crashin’ if one fails—that’s production-ready.
Quick Cheat Sheet for Shell Scripting Concepts
Here’s a lil’ table to bookmark before your next screen. Keep it handy!
| Concept | Syntax/Command | When to Use | Interview Level |
|---|---|---|---|
| Shebang | #!/bin/bash |
First line, always | Beginner |
| Variables | NAME="value"; echo $NAME |
Store stuff, quote ‘em | Beginner |
| Conditionals | if [ "$VAR" = "x" ]; then |
Decision makin’ | Beginner |
| For Loop | for i in list; do ... done |
Fixed lists like servers | Intermediate |
| Error Handling | set -Eeuo pipefail |
Every serious script | Intermediate |
| Trap | trap 'cleanup' EXIT ERR |
Rollbacks, cleanup | Advanced |
| Cron Scheduling | 0 2 * * * /script.sh >> log 2>&1 |
Automate tasks, log it | Intermediate |
FAQ: Stuff You’re Prob’ly Wonderin’ About
- Is Bash still a thing in 2026, or should I just learn Python?
Heck yeah, Bash is still king for system automation, cron jobs, and minimal environments like Docker. Python’s dope for complex logic, but Bash runs everywhere Linux does. Learn both, start with Bash. - What’s the one thing I gotta know for interviews?
Error handlin’.set -Eeuo pipefailandtrap. Silent fails can tank deployments or break compliance rules. Nail this, and you’re golden. - How many questions will I get in a Linux admin interview?
Expect 3–5 for sysadmin gigs, plus a 20–30 minute live codin’ round for senior roles. Big companies always test practical scripting.
Final Pep Talk: You’ve Got This!
Here’s the straight-up truth: the folks pullin’ $130K+ for “automatin’ infrastructure” ain’t doin’ magic. They’re just takin’ commands, slappin’ ‘em in a script, and lettin’ the machine grind. Shell scripting ain’t about bein’ a coder—it’s about refusin’ to do dumb work twice. Every script ya write is time ya get back. And every script ya explain in an interview? That’s cash in your pocket.
So, master that set -Eeuo pipefail. Know your special vars like $?. Memorize those live codin’ scripts. When they hand ya a terminal, just remember: you’ve been here before. Go crush it, fam! If ya got more questions or wanna chat Linux, hit me up in the comments. Let’s get ya to that next level!

1 Why would someone choose CLI over GUI?
The Command Line Interface( CLI) is truly lightweight, fast, and scriptable.
It allows precise control and is essential for automation using shell scripting. While GUIs are user-friendly, CLIs are truly necessary for developers and sysadmins.
2 Can you explain the difference between absolute and relative paths in Linux?
Absolute path – Full path from the root directory.
Example: / home/ user/ docs / file.txt
Relative path – Path from your current working directory.
illustration./ croakers / file.txt
These are must-know shell scripting interview questions, especially when handling file and directory operations.