30 Shell Scripting Interview Questions and Answers for DevOps Engineers (0โ3 Years Experience)
If you’re preparing for a DevOps interview, chances are you’ll be asked at least a few shell scripting questions.
Many candidates mention shell scripting on their resume, but during interviews they struggle to explain basic concepts like positional parameters, loops, pipes, redirection, or common Linux commands.
The good news is that most interviewers don’t expect complex scripting knowledge from candidates with 0โ3 years of experience. They usually want to check whether you understand the basics and whether you’ve actually used shell scripting in real projects.
In this article, we’ll go through some of the most commonly asked Shell Scripting Interview Questions along with simple explanations and practical examples.
1. What is #!/bin/bash in a Shell Script?
Answer: This line is called a shebang. It tells the operating system to execute the script using the Bash shell. You will write this shebang line at the starting of the script.
Example:
#!/bin/bash
echo "hello World"
Without this line, the system may try to run the script using a different shell.
2. What is '$0' in Shell Scripting?
Answer: '$0' stores the name of the currently running script.
Example:
$ echo $0
output:
./backup.sh
This is commonly used for logging and debugging purposes.
3. What are ‘$1', ‘$2', '$3' Parameters?
Answer: These are positional parameters used to access values passed to a script.
Example:
./script.sh Devops Linux Terraform
echo $0
echo $1
echo $2
echo $3
Output:
script.sh
Devops
Linux
Terraform
4. What is '$#'?
Answer: '$#' returns the total number of arguments passed to a script.
Example:
echo $#
</-- If three arguments are provided, the output will be: -->
3
5. What is '$?'?
Answer: '$?' stores the exit status of the last executed command.
0means success- Non-zero means failure
Example:
mkdir test
echo $?
This is frequently used in automation scripts to verify whether a command executed successfully.
6. What is the 'if' Statement?
Answer: The 'if' statement is used to execute code based on a condition.
Example:
if [ $age -gt 18 ]
then
echo "Eligible"
fi
In DevOps projects, if statements are often used to validate deployments and check server status.
7. What is the 'elif' Statement?
Answer: 'elif' allows you to check multiple conditions.
Example:
if [ $num -eq 1 ]
then
echo "One"
elif [ $num -eq 2 ]
then
echo "Two"
fi
8. What is a 'case' Statement?
Answer: A 'case' statement is a cleaner alternative to multiple if-else conditions.
Example:
case $action in
start)
echo "Starting Service"
;;
stop)
echo "Stopping Service"
;;
esac
This is commonly seen in service management scripts.
9. What is a 'for' Loop?
Answer: A 'for' loop is used when you need to repeat a task multiple times.
Example:
for server in app1 app2 app3
do
echo $server
done
In DevOps we often use 'for' loops to perform operations across multiple servers.
10. What is a 'while' Loop?
Answer: A 'while' loop runs until a condition becomes false.
Example:
while [ $count -le 5 ]
do
echo $count
((count++))
done
11. What is the 'break' Statement?
Answer: The 'break' statement immediately exits a loop.
Example:
if [ $count -eq 5 ]
then
break
fi
12. What is the 'continue' Statement?
Answer: 'continue' skips the current iteration and moves to the next one.
Example:
if [ $count -eq 5 ]
then
continue
fi
13. Why are Functions Used in Shell Scripting?
Answer: Functions help organize code and avoid repetition.
Example:
backup() {
echo "Taking Backup"
}
Functions make scripts easier to maintain and troubleshoot.
14. What is the 'read' Command?
Answer: The 'read' command accepts user input.
Example:
read username
echo $username
15. What is the 'echo' Command?
Answer: The 'echo' command displays text on the terminal.
Example:
echo "Deployment Started"
It is one of the most frequently used commands in shell scripts.
16. What is the 'expr' Command?
Answer: The 'expr' command is used for arithmetic calculations.
Example:
expr 10 + 5
Output:
15
17. What is the 'cut' Command?
Answer: The 'cut' command extracts specific columns from text.
Example:
echo "Avinash,DevOps" | cut -d',' -f2
Output:
DevOps
18. What is the 'sort' Command?
Answer: The 'sort' command arranges data alphabetically or numerically.
Example:
sort users.txt
19. What is the 'uniq' Command?
Answer: The 'uniq' command removes duplicate entries from sorted data.
Example:
sort users.txt | uniq
20. What is the 'wc' Command?
Answer: The 'wc' command counts lines, words, and characters.
Example:
wc file.txt
21. What is the 'head' Command?
Answer: The 'head' command displays the first few lines of a file.
Example:
head file.txt
This is useful when reviewing large log files.
22. What is the 'tail' Command?
Answer: The 'tail' command displays the last few lines of a file.
Example:
tail file.txt
23. What is the 'tee' Command?
Answer: The 'tee' command saves output to a file and displays it on the screen at the same time.
Example:
ls | tee output.txt
24. What is the 'xargs' Command?
Answer: 'xargs' converts input into command-line arguments.
Example:
cat files.txt | xargs rm
25. What is the 'export' Command?
Answer: The 'export' command creates environment variables available to child processes.
Example:
export ENV=production
26. What is Input and Output Redirection?
Answer: Redirection allows you to send output to files or take input from files.
Example:
ls > output.txt
ls >> output.txt
cat < input.txt
27. What are Pipes ("|") in Linux?
Answer: A pipe sends the output of one command directly into another command.
Example:
cat logs.txt | grep ERROR
This is heavily used in Linux administration and DevOps tasks.
28. What are Environment Variables?
Answer: Environment variables store configuration values used by applications and the operating system.
Example:
HOME
PATH
USER
Check value:
echo $HOME
29. What is Command Substitution?
Answer: Command substitution allows you to store command output inside a variable.
Example:
CURRENT_DATE=$(date)
echo $CURRENT_DATE
30. What is the Role of Shell Scripting in DevOps?
Answer: Shell scripting is one of the most important skills for a DevOps engineer.
It helps automate repetitive tasks such as:
– Application deployment
– Backup automation
– Log monitoring
– User management
– Health checks
– CI/CD pipeline automation
– Infrastructure management
Instead of manually performing these tasks every day, engineers write scripts that execute them automatically.
Final Thoughts of this Article
For most DevOps interviews, interviewers are not looking for complicated shell scripts. They want to see whether you understand the basics and whether you’ve used shell scripting to solve real-world problems.
Focus on understanding commands, loops, conditions, pipes, redirection, and automation use cases. If you can explain each concept with a small practical example, you’ll be in a strong position for junior and mid-level DevOps interviews.
From all of us at PrepForCareers.com, we wish you success in your upcoming interviews and career journey. Good luck!
All the information shared in this article has been compiled from trusted industry resources, technical references, and practical knowledge. If you have any questions or would like to learn more, feel free to leave a comment or contact us through our Contact Us page.
