Shell Scripting Interview Questions and Answers

Shell scripting interview questions for 2026: bash automation, debugging, awk, set -euo pipefail, ShellCheck—grouped practical answers.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Shell Scripting Interview Questions and Answers

Shell scripting interviews test whether you can automate real admin tasks—waiting on prompts, logging, file checks, debugging flags, and small one-liners under pressure. Below are practical questions grouped by scenario; many have more than one valid approach. For operating system interview questions (processes, memory, scheduling, Linux kernel concepts), see operating system interview questions. For C and C++ interview questions and gdb/sanitizer debugging context on Linux, see C and C++ interview questions.

NOTE
Scripting answers vary by shell and environment. The examples below assume bash on Linux unless noted otherwise.

Interview context and how to prepare

What do shell scripting interviews test?

Practical automation under constraints:

  • File tests, loops, functions, exit codes
  • Text processing (awk, sed, grep patterns)
  • Debugging (bash -x, logging with logger)
  • Safe scripting habits for production cron/systemd jobs

Interactive automation and expect

How can you create a script that will wait for specific output and hence will act according to it? - for instance, wait for “username: ” before sending the username.
Linux provides a tool that “expects” a specific string and sending new commands in response which called “expect”.

Logging, debugging, and safe execution

You want to add logger to your script so how can you send logging messages to the /var/log/messages for your script “*MyCoolScript*”?

Now to write to the messages file, you can use the logger tool which is the syslogd api and send log messages -

logger -t MyCoolScript Starting Application….
You have a bash script that does not produce the expected result so how can you debug it?

In order to debug a bash shell script, you need to add “-x” to the shell execute line -

“#!/bin/bash -x”
You need to create a backup script called backupMyFiles which will run every hour. How do you ensure the script is not already running and exit with a clear message if a previous run is still active?

To check if your script is currently running in the system - you can do it with ps

#!/bin/bash
cmd=namedScript
runningProcs=`ps --no-headers -C${cmd}`
count=`echo $runningProcs|wc -l`

if [ $count -gt 1 ]; then
   echo "Previous $cmd is still running."
   exit 1
fi

File permissions and validation scripts

Write a shell script that checks if a file (as an argument) has write permissions and accordingly if it is available print “*write access approved*” else print “*no write access*”.
#!/bin/bash filename ="$1" if [ -w "$filename" ] then echo "write access approved" else echo "no write access”; fi
Write a script that receives one parameters (file name) and checks if the file exists or not - If it does, print “*Roger that!*” else, print “*Huston we’ve got a problem!*”
#!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "Roger that!" else echo "Huston we’ve got a problem" fi
Write a script that checks if a file, given as an argument, has more than 10 lines or not, if it does - print “*Over 10*”, else print “*Less than 10*”
#!/bin/bash count=cat $1 | wc -l if [ "$count" -gt "10" ] ; then echo "Over 10" else echo "Less than 10" fi

Users, processes, and system introspection

You have a regular user access to a server, with no root permissions, but you need to create a script that requires root permissions to run - how can you manipulate the system to think that you have root permissions, without a real superuser access?
Linux provides a tool called fakeroot that allows you to run a “fake root shell” that will present you as root and your id as 0, this will make the system believe that you have root access for the current run.
Write a script that goes over all users on the system and prints each user last login date, or No data for that user when last login is unknown.
#!/bin/sh for i in cat /etc/passwd | awk -F: '{print $1; }' ; do last=last $i | head -n 1; if [ "$last" != "" ]; then echo last $i | head -n 1; else echo "No data for $i" fi done
How can you check what are the most common commands that you have used in the Linux shell?

You can get this information from the history command and sort it by most used.

$ history | awk '{h[$2]++}END{for (c in h){print h[c] " " c}}' | sort -nr | head
3 history
2 ls
1 hostname
1 cd
Create a script called *KillUserProcs* that will get a username as an input and will kill all his processes.
#!/bin/bash kill -9 ps aux|awk -v var=$1 '$1==var { print $2 }'

Text processing and one-liners

Using perl, write a command that will print all the IPs, Bcasts and Masks configured on the server line by line.

See also: print all the IPs with ip command.

You need to extract the IPs from the ifconfig first, then run on each line and get the required information -

ifconfig -a | perl -n -l -e '/ addr:([^ ].+)/ and print $1'
Create a Fibonacci function (Fn=Fn-1+Fn-2) using awk (until F20).

Use awk as follows:

awk 'BEGIN { fa=1; fb=1; while(++i<=20) { print fa; ft=fa; fa=fa+fb; fb=ft }; exit}'


Network and utility scripts

Write a script that goes to the Whatismyip website and then prints Your IP is with the result from the site.

Visit whatismyip.org from the script:

#!/bin/sh ip=links --source http://www.whatismyip.org/ echo -n "Your IP is: " echo $ip

Create a small calculator in bash script which will have an internal function “*dosomething*” that will receive a math function as an input - mycalc 4+4\*4.

#!/bin/bash function dosomething { echo "${1}"|bc -l; }

dosomething $1

Modern bash practices

Why do teams use set -euo pipefail in bash scripts?

Common safety flags:

  • -e — exit on first command failure
  • -u — treat unset variables as errors
  • -o pipefail — pipeline fails if any stage fails

Example header:

bash
#!/usr/bin/env bash
set -euo pipefail

Combine with explicit trap handlers for cleanup in long-running jobs.

How does ShellCheck help in interviews and on the job?

ShellCheck statically analyzes bash/sh scripts for:

  • Quoting bugs and word-splitting traps
  • Undefined variables (especially after refactors)
  • Deprecated or non-portable constructs

In interviews, mentioning ShellCheck signals you have maintained production cron jobs without silent failures.

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …