Shell Scripting Interview Questions and Answers

1️⃣ List Some Commonly Used Shell Commands
Some frequently used shell commands include:
ls
– List files and directoriescd
– Change directorypwd
– Print working directorymkdir
– Create a new directoryrm
– Remove files or directoriescp
– Copy files or directoriesmv
– Move or rename filescat
– Display file contentsecho
– Print text to the terminalgrep
– Search for patterns in a filechmod
– Change file permissionschown
– Change file ownershipps
– Display currently running processeskill
– Terminate processes
2️⃣ Write a Shell Script to List All Processes
You can use the ps
command to list processes:
bashCopyEdit#!/bin/bash
# Display all running processes
ps -f
# Print only the second column (process IDs)
ps -ef | awk '{print $2}'
3️⃣ Write a Script to Print Only Errors from a Remote Log
You can fetch a remote log file using curl
and filter errors using grep
:
bashCopyEditcurl -s <log_url> | grep "ERROR"
curl
fetches the log filegrep "ERROR"
filters out only the error messages
4️⃣ Write a Shell Script to Print Numbers Divisible by 3 & 5 but Not 15
bashCopyEdit#!/bin/bash
for i in {1..100}; do
if { [ $((i % 3)) -eq 0 ] || [ $((i % 5)) -eq 0 ]; } && [ $((i % 15)) -ne 0 ]; then
echo $i
fi
done
The script loops from 1 to 100.
It checks if a number is divisible by 3 or 5 but not 15.
5️⃣ Write a Script to Count the Number of ‘S’ in "Mississippi"
bashCopyEdit#!/bin/bash
x="mississippi"
echo "$x" | grep -o "s" | wc -l
grep -o "s"
extracts all occurrences of "s".wc -l
counts the occurrences.
6️⃣ How Do You Debug a Shell Script?
Use the set -x
command to enable debugging:
bashCopyEditset -x # Enable debugging
To disable debugging:
bashCopyEditset +x # Disable debugging
7️⃣ What is Crontab in Linux? Example Usage?
Crontab is a scheduler that allows you to run scripts at specific times.
Example: Schedule a script to run daily at 6 PM:
bashCopyEditcrontab -e
Add the following line:
bashCopyEdit0 18 * * * /path/to/script.sh
This executes script.sh
every day at 6 PM.
8️⃣ How Do You Open a File in Read-Only Mode?
Use the following command:
bashCopyEditvim -R filename.sh
-R
opens the file in read-only mode.
9️⃣ Difference Between Soft Link and Hard Link
Feature | Soft Link (Symbolic Link) | Hard Link |
Points to | Another file | File’s inode (actual data) |
Works across file systems? | Yes | No |
File existence | If the original file is deleted, the link breaks | Even if the original file is deleted, the hard link remains |
Example | ln -s file1 link1 | ln file1 link1 |
Example Use Case:
Soft Links: Used for pointing to configuration files, e.g.,
/usr/bin/python → /usr/bin/python3
.Hard Links: Used to create backup copies of files without duplication.
🔟 break
and continue
Statements in Loops
break
: Exits the loop immediately.continue
: Skips the current iteration and moves to the next.
Example:
bashCopyEdit#!/bin/bash
for i in {1..10}; do
if [ $i -eq 5 ]; then
break # Stops the loop at 5
fi
echo "Number: $i"
done
bashCopyEdit#!/bin/bash
for i in {1..10}; do
if [ $i -eq 5 ]; then
continue # Skips number 5
fi
echo "Number: $i"
done
1️⃣1️⃣ Disadvantages of Shell Scripting
Slow execution: Shell scripts are slower than compiled programs.
Limited debugging tools: Hard to trace errors compared to languages like Python.
Portability issues: Some commands behave differently on different shells (Bash, Zsh, etc.).
Security risks: Improperly written scripts may expose security vulnerabilities.
1️⃣2️⃣ Types of Loops in Shell Scripting
For loop → Used when the number of iterations is known
While loop → Runs while a condition is true
Until loop → Runs until a condition becomes true
Example of a for loop:
bashCopyEditfor i in {1..5}; do
echo "Iteration: $i"
done
1️⃣3️⃣ Is Bash Dynamically or Statically Typed? Why?
Bash is dynamically typed because variables do not require explicit data types.
Example:
bashCopyEditx=5 # Integer x="hello" # Now it's a string
Unlike statically typed languages (C, Java), Bash does not enforce type checking.
1️⃣4️⃣ Explain a Network Troubleshooting Utility
One of the most common utilities is traceroute, which tracks the path packets take to a destination.
bashCopyEdittraceroute google.com
- Helps identify network latency and packet loss.
1️⃣5️⃣ How to Sort a List of Names in a File?
bashCopyEditsort names.txt
Sorts names alphabetically.
Use
-r
for reverse sorting:bashCopyEditsort -r names.txt
1️⃣6️⃣ How to Manage Large System Log Files?
1️⃣ Rotate logs automatically using logrotate:
bashCopyEditsudo nano /etc/logrotate.d/custom_log
Add:
luaCopyEdit/var/log/myapp.log {
weekly
rotate 4
compress
missingok
notifempty
}
Rotates logs weekly and keeps last 4 copies.
Compresses old logs to save space.
2️⃣ Manually clear logs:
bashCopyEdit> /var/log/syslog
- This clears the log file without deleting it.
🚀 Conclusion
Shell scripting is an essential skill for Linux administrators, DevOps engineers, and AWS Cloud Engineers. Understanding these interview questions will help you tackle real-world challenges in automation and system administration.
Subscribe to my newsletter
Read articles from Prashant Mahamuni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
