Shell Scripting Interview Questions and Answers – A Beginner’s Guide
If you're preparing for a Linux admin, DevOps, or automation role, shell scripting is often a key skill tested in interviews. This blog covers common shell scripting interview questions with simple, clear answers to help you prepare confidently.
1. Basic Shell Scripting Questions
Q1. What is a shell script?
Answer:
A shell script is a text file containing a series of Linux commands that are executed sequentially. It helps automate repetitive tasks.
Example:
#!/bin/bash
echo "Hello, World!"
Q2. What is the shebang (#!) line?
Answer:
The shebang (#!) tells the system which interpreter (like /bin/bash) should execute the script.
Example:
#!/bin/bash # Uses Bash shell
#!/bin/sh # Uses the default shell
Q3. How do you make a script executable?
Answer:
Use chmod +x to give execute permission:
chmod +x script.sh
./script.sh # Run the script
Q4. How do you add comments in a shell script?
Answer:
Use # for single-line comments:
# This is a comment
echo "Hello" # Inline comment
2. Variables and Input/Output
Q5. How do you define and use a variable?
Answer:
name="Alice" # Define
echo "Hello, $name" # Use (with $)
Q6. How do you take user input in a script?
Answer:
Use read:
echo "Enter your name:"
read username
echo "Hello, $username!"
Q7. What is the difference between $@ and $*?
Answer:
$@→ Treats each argument as a separate word.$*→ Treats all arguments as a single string.
Example:
./script.sh arg1 arg2
# $@ = "arg1" "arg2"
# $* = "arg1 arg2"
3. Conditional Statements & Loops
Q8. How do you write an if-else statement?
Answer:
if [ condition ]; then
# Code
else
# Code
fi
Example:
if [ -f "file.txt" ]; then
echo "File exists!"
else
echo "File not found."
fi
Q9. How do you write a for loop?
Answer:
for i in 1 2 3; do
echo "Number: $i"
done
Example (Loop through files):
for file in *.txt; do
echo "Processing $file"
done
Q10. How do you write a while loop?
Answer:
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
4. Functions & Command-Line Arguments
Q11. How do you define a function?
Answer:
function greet() {
echo "Hello, $1!"
}
greet "Alice" # Output: Hello, Alice!
Q12. How do you use command-line arguments?
Answer:
$0→ Script name$1,$2→ First, second argument$#→ Total arguments
Example:
echo "First argument: $1"
echo "Total arguments: $#"
5. File & Text Processing
Q13. How do you check if a file exists?
Answer:
Use -f:
if [ -f "file.txt" ]; then
echo "File exists!"
fi
Q14. How do you search for text in a file?
Answer:
Use grep:
grep "error" logfile.txt
Q15. How do you replace text in a file?
Answer:
Use sed:
sed -i 's/old/new/g' file.txt
6. Error Handling & Debugging
Q16. How do you exit a script if a command fails?
Answer:
Use set -e:
#!/bin/bash
set -e # Exit on error
rm non_existent_file # Script stops if this fails
Q17. How do you debug a shell script?
Answer:
Run with -x for debugging:
bash -x script.sh
7. Real-World Scenarios
Q18. Write a script to find and delete old log files.
Answer:
#!/bin/bash
find /var/log -name "*.log" -mtime +30 -delete
Q19. Write a script to check disk space and alert if low.
Answer:
#!/bin/bash
threshold=90
usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ $usage -gt $threshold ]; then
echo "Warning: Disk space low!"
fi
8. Best Practices
Q20. What are some shell scripting best practices?
Answer:
✔ Use comments (#) to explain code.
✔ Quote variables ("$var") to avoid errors.
✔ Use [[ ]] instead of [ ] for safer comparisons.
✔ Test scripts in small steps.
Final Tips for Interviews
- Practice writing scripts (file operations, loops, conditionals).
- Understand exit codes (
0= success,non-zero= failure). - Be ready for real-world problems (log parsing, automation).
Conclusion
Shell scripting is a must-know skill for Linux and DevOps roles. These interview questions and answers cover basics to real-world scenarios.
🚀 Keep practicing, and you’ll ace your interview!
Got more questions? Ask in the comments! 💬🐧
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.