Shell scripting is a way to automate tasks in Unix-based systems using command-line instructions. It started in the 1970s when Stephen Bourne created the Bourne shell at Bell Labs. This scripting language quickly became essential for system administrators and developers to manage repetitive tasks efficiently. Today, it is a must-have skill in many tech roles. If you are preparing for interviews, understanding how shell scripts work and being able to explain them clearly is very important. That is why we have put together 30+ shell scripting interview questions with clear and practical answers to help you get ready.
Fun Fact – Shell scripting has been around for over 50 years and is still used by millions of developers to automate daily tasks.
Note – We have divided the shell scripting interview questions into basic-level, intermediate-level, and advanced-level sections to help you prepare step by step for any interview.
Basic Level Shell Scripting Interview Questions
If you are just starting out, these shell scripting questions for interview will help you build a strong foundation.
- How do you make a shell script executable?
You make a shell script executable by using the chmod +x filename.sh command. This adds execute permission to the script file.
- What is the difference between == and -eq in shell scripting?
== is used for comparing strings. -eq is used for comparing integers. Mixing them will lead to unexpected results or errors in conditions.
- How can you schedule a script to run every day at midnight?
Use crontab -e and add:
0 0 * * * /path/to/script.sh
This tells cron to run the script daily at 12:00 AM.
- How do you handle command line arguments in shell scripting?
Arguments are accessed using $1, $2, and so on. $# gives the number of arguments. $@ lists them all.
Example:
echo “First arg is $1”
- How do you use a for loop in a shell script?
Here’s a basic example:
for file in *.txt
do
echo “$file”
done
It loops through all .txt files in the directory.
- What is the use of chmod +x command?
It adds execute permission to a script. Without it, I can’t run the script directly.
Note – These interview questions on scripting focus mainly on shell scripting basics, suitable for freshers and entry-level roles.
Intermediate Level Interview Questions for Shell Scripting
These are intermediate-level shell scripting questions and answers that test your ability to write, debug, and understand scripts.
- Explain the difference between su and sudo in scripting context.
su switches to another user account, often root. It needs a password. sudo runs a single command as another user. It is safer and logs activity. I usually prefer sudo in scripts.
- Explain the difference between cron and at commands.
cron is used for scheduling repetitive tasks like daily backups. at runs a command once at a specified time. Both are useful, but cron is better for regular jobs.
- What is a here document in shell scripting?
A here document sends multiple lines of input to a command.
Example:
cat << EOF
This is input
EOF
It is often used to write files or pass content.
- How do you use awk in shell scripting?
awk is a powerful text processing tool. It works well with columns.
Example:
awk ‘{print $1}’ file.txt
This prints the first column from each line.
- What is the difference between source and executing a script directly?
Using source runs the script in the current shell. Changes (like exported variables) stay. Executing normally runs it in a new shell, so changes don’t persist.
- How do you validate numeric input in shell scripts?
Use a regular expression or case:
if [[ “$input” =~ ^[0-9]+$ ]]; then
echo “Valid number”
fi
This checks if input contains only digits.
Advanced Level Shell Scripting Interview Questions for Experienced
Here is a list of UNIX shell scripting interview questions for experienced professionals to test deep scripting knowledge.
- How do you manage environment variables in Python scripts?
You can read and set environment variables using Python’s os module.
Example:
import os
os.environ[“MODE”] = “production”
print(os.getenv(“MODE”))
It is also common to load variables from a .env file using third-party libraries like python-dotenv.
- How do you check if a service is running using shell script?
You can use systemctl or pgrep depending on the system.
Example with systemd:
systemctl is-active –quiet nginx && echo “Running” || echo “Not running”
This gives a simple true/false check.
- How do you redirect both stdout and stderr to a file?
Use this syntax:
command > output.log 2>&1
This saves both standard output and error to output.log. The 2>&1 means redirect stderr (2) to the same place as stdout (1).
- How can you debug a shell script line by line?
Add set -x at the top of your script.
It shows each command before it runs. For deeper analysis, I also echo variables at key steps.
Use set +x to stop tracing.
- What is the role of environment variables in scripting?
Environment variables store values like paths, config names, and tokens.
Scripts can read them using $VARIABLE_NAME.
They make scripts portable and easy to change without editing the script body.
- How do you monitor a log file for changes using a script?
Use tail -F or inotifywait.
Example with tail:
tail -F /var/log/syslog | while read line
do
echo “$line”
done
This keeps watching the log in real time.
With inotify-tools, I can trigger actions only when changes happen.
Shell Programming Interview Questions
These shell programming questions are designed to test your understanding of scripting logic, control structures, and command-line operations in real scenarios.
- What is the use of chomp() in Perl?
chomp() removes the newline character (\n) from the end of strings.
Example:
my $line = <STDIN>;
chomp($line);
It is useful when reading user input or file lines.
- How do you check disk usage and alert if usage is above threshold?
Use df and awk in a script.
Example:
usage=$(df / | awk ‘NR==2 {print $5}’ | tr -d ‘%’)
if [ “$usage” -gt 80 ]; then
echo “Disk usage is above 80%”
fi
This checks root partition usage.
- What is the purpose of shift in shell scripting?
shift moves positional parameters left.
After shift, $2 becomes $1, and so on.
Useful in loops when processing many arguments.
- How can you handle errors in shell scripts?
This is one of the most important shell scripting programming interview questions for experienced professionals.
Use set -e to stop on errors.
Check command status with $?.
You can also use traps for cleanup.
- How do you declare and use arrays in Perl?
Declare arrays with @.
Example:
my @colors = (“red”, “blue”, “green”);
print $colors[0];
Use @array to access all elements.
Shell Scripting Questions for Practice
Let’s go through some scripting interview questions that are perfect for sharpening your skills.
- Write a script to monitor a service and restart it if it stops.
You can use systemctl in the script:
service=”nginx”
if ! systemctl is-active –quiet “$service”; then
systemctl restart “$service”
echo “$service restarted”
fi
This checks if the service is active. If not, it restarts it.
- How do you delete blank lines from a file using a script?
You can use sed like this:
sed -i ‘/^$/d’ filename.txt
It removes all lines that are completely empty.
- What is the difference between hard links and soft links?
Hard links point directly to file data. Deleting the original file doesn’t remove the content.
Soft links (symbolic links) point to the filename. If the original is deleted, the link breaks.
- What is the purpose of shebang (#!) in shell scripts?
Shebang tells the system which interpreter to use.
For example, #!/bin/bash runs the script using Bash.
Without it, the script may not work as expected.
Shell Scripting Language Viva Questions
Here are some commonly asked shell scripting interview questions often included in viva exams.
- How do you parse JSON using shell scripting?
You can use jq, a command-line JSON processor.
Example:
cat data.json | jq ‘.name’
It reads and extracts values from a JSON file. jq must be installed.
- How do you automate CI/CD tasks using Python?
I use Python scripts to trigger builds, run tests, and deploy code.
Modules like subprocess, requests, and os are helpful.
I also integrate scripts into Jenkins or GitHub Actions for full automation.
- How do you check the exit status of a command in shell?
Use the special variable $?.
It returns 0 if the command was successful, or a non-zero value if it failed.
Example:
ls file.txt
echo $?
If the file doesn’t exist, it prints a non-zero code.
- How would you create an infinite loop in a shell script?
You can use:
while true
do
echo “Running…”
done
This loop runs endlessly until manually stopped.
- What is the significance of exit codes in scripting?
Exit codes tell you if a command worked or failed.
They help in conditional checks and debugging.
In automation scripts, I often use them to control flow or trigger alerts.
Also Read - Top 25+ CI/CD Interview Questions and Answers
Other Important Shell Scripting Interview Questions
This is a useful set of shell scripting interview questions that cover tricky areas often asked in interviews but not always practiced enough.
Shell Scripting in Linux Interview Questions
This section includes interview questions on Linux shell scripting that are commonly asked in system admin and DevOps interviews across various industries.
- Write a Python script to read a file and print each line.
- Explain scalar vs list context in Perl.
- Write a Python script to ping a list of IP addresses.
- How do you use subprocess in Python for automation?
- What happens if you run a script without a shebang?
Note – Interview questions on shell scripting in Linux often cover command usage, script writing, file handling, loops, conditionals, and real-time automation tasks.
Also Read - Top 15+ Python Automation Interview Questions and Answers
Linux Scripting Interview Questions
Here are some commonly asked Linux scripting questions that help test your ability to write, debug, and automate tasks in a Linux environment.
- Explain the use of case statement in shell scripting.
- Write a Perl script to replace text in a file.
- What is the difference between $* and $@?
- How do you monitor a log file for changes using a script?
- How can you append to a file in a shell script?
Interview Questions for UNIX Shell Scripting
These UNIX scripting interview questions are designed to test your knowledge of shell commands, scripting logic, and automation techniques used in UNIX systems.
- What are regular expressions in Perl used for?
- What does $? return in a shell script?
- How do you comment multiple lines in a shell script?
- What is the use of set -e in scripts?
- How do you compare strings in a shell script?
Note – UNIX scripting questions often include topics like file handling, process management, loop structures, conditional statements, and practical shell script use cases.
Also Read - Top 25+ Unix Interview Questions and Answers
Python Scripting Interview Questions
Let’s go through some interview questions for Python scripting that test your automation skills, logic building, and practical use of Python in scripting tasks.
- What is a subshell and how is it used?
- What are positional parameters in shell scripting?
- What are some common security practices in writing shell scripts?
- How is shell scripting used in DevOps pipelines?
- How do you pass arguments to a shell script?
Python Scripting Interview Questions for DevOps
These scripting interview questions Python professionals face in DevOps roles focus on automation, CI/CD integration, and managing infrastructure through efficient scripting.
- Write a script to back up a directory.
- How do you write a function in shell scripting?
- How do you terminate a background process started by a script?
- What are some uses of Python scripting in automation?
- What is the purpose of trap command in shell scripting?
Shell Scripting Interview Questions for DevOps
These shell scripting interview questions are tailored for DevOps roles.
- How do you check if a file exists in a shell script?
- How do you handle file and directory permissions in scripts?
- Write a script to archive logs older than 7 days.
- How do you validate configuration files using a shell script?
- How do you use shell scripts in Jenkins pipelines?
Also Read - Top 25+ DevOps Interview Questions and Answers
Bash Scripting Interview Questions
Here are some important bash shell interview questions that test your understanding of core scripting concepts, command execution, and automation techniques in Bash.
- How do you define and call a function in Bash?
- What is the difference between [[ and [ in Bash?
- How do you declare and use an array in Bash?
- How do you perform arithmetic operations in Bash?
- What is the difference between set -x and set +x?
Perl Interview Questions
- What is the difference between my, our, and local in Perl?
- How do you use regular expressions for pattern matching in Perl?
- How do you open and read a file in Perl?
- What is the use of split() and join() in Perl?
- How do you handle command-line arguments in Perl scripts?
Tips to Prepare for Shell Scripting Interview
Preparing well for shell scripting interview questions can help you feel confident and perform better.
- Understand the basics: Know how variables, loops, and conditions work in shell scripts.
- Practice common commands: Use grep, awk, sed, cut, and find regularly.
- Write and run scripts: Practice real scripts for file handling, service checks, and backups.
- Solve shell scripting interview questions: Go through questions from actual interviews and try writing answers yourself.
- Learn how to debug: Use set -x and echo statements to trace script flow and fix issues.
- Read logs and errors: Learn to find problems using logs or stderr redirection.
- Practice one-liners: Try solving tasks using single-line shell commands for quick problem-solving.
Wrapping Up
These 30+ shell scripting interview questions cover real topics you are likely to face in technical interviews. Practice regularly, write your own scripts, and understand each concept well. This will help you stay prepared and confident.
Looking for jobs? Visit Hirist – an online job portal for IT professionals. Find top Shell Scripting jobs in India quickly and easily on Hirist.
FAQs
They include file handling, loops, conditions, command substitution, error handling, and script execution basics like using chmod, shebang, and input arguments.
On average, shell scripting roles in India offer ₹24.3 lakhs, depending on experience, skills, and additional knowledge of Linux or DevOps tools.
Questions often involve writing small scripts, explaining logic flow, using pipes, loops, conditions, and solving file or system-level automation problems.
Expect questions on bash, cron, file permissions, signal trapping, script debugging with set -x, and handling input/output using redirection or pipes.
They usually cover real-world scripting tasks like backups, log rotation, text processing, user management, and automation using standard Unix commands.