Top 20 Shell Script Interview Questions and Answers Every Linux Engineer Should Know
This article compiles the most common shell script interview questions, covering fundamentals such as script purpose, default login shells, variable types, redirection, control structures, debugging, file testing, and special Bash variables, providing concise answers and practical code examples for each topic.
Although Python is now common in operations, many companies still ask Linux cloud engineers about shell scripts , which help automate many tasks.
Q:1 What is a shell script and is it necessary?
A shell script is a text file containing one or more commands, allowing system administrators to bundle multiple commands into a single file for routine tasks.
Q:2 What is the default login shell and how to change a user’s login shell?
The default login shell in Linux is /bin/bash. Use the chsh command to change it, e.g.:
# chsh <username> -s <new_shell>
# chsh linuxtechi -s /bin/shQ:3 What types of variables can be used in shell scripts?
Two types: system-defined variables (usually uppercase, viewable with set) and user-defined variables (accessed with $variable).
Q:4 How to redirect both standard output and error to the same location?
Method 1: 2>&1 # e.g., ls /usr/share/doc > out.txt 2>&1 Method 2: &> # e.g., ls /usr/share/doc &> out.txt Q:5 How to nest "if" statements in a shell script?
if [ condition ]
then
# commands
else
if [ condition ]
then
# commands
else
# commands
fi
fiQ:6 What is the purpose of the $? variable?
It holds the exit status of the last command; 0 means success, non‑zero indicates failure.
# ls /usr/bin/shar
/usr/bin/shar
# echo $?
0Q:7 How to compare two numbers in a shell script?
#!/bin/bash
x=10
y=20
if [ $x -gt $y ]
then
echo "x is greater than y"
else
echo "y is greater than x"
fiQ:8 What does the break command do?
It exits the current loop (e.g., while or until).
Q:9 What does the continue command do?
It skips the remainder of the current loop iteration and proceeds with the next iteration.
Q:10 What is the syntax of the case statement?
case variable in
value1)
# commands
;;
value2)
# commands
;;
esacQ:11 What is the syntax of a while loop?
while [ condition ]
do
# commands
doneQ:12 How to make a script executable? # chmod a+x myscript.sh Q:13 What is the purpose of #!/bin/bash ?
It is the shebang line that tells the system to execute the script using /bin/bash.
Q:14 What is the syntax of a for loop?
for variable in list
do
# commands
doneQ:15 How to debug a shell script?
Use the -x option ( sh -x myscript.sh) or -nv ( sh -nv myscript.sh).
Q:16 How to compare strings in a shell script?
Use the test command or [ ] with string operators.
Q:17 What special variables exist in Bourne shell (bash)?
$0 script name
$1 first argument
$2 second argument
...
$# number of arguments
$* all arguments as a single wordQ:18 How to test files in a shell script?
Test Usage
-d file true if file exists and is a directory
-e file true if file exists
-f file true if file exists and is a regular file
-r file true if file is readable
-s file true if file is not empty
-w file true if file is writable
-x file true if file is executableQ:19 How to write comments in a shell script?
#!/bin/bash
# This is a comment
echo "I am logged in as $USER"Q:20 How to read input from the terminal in a shell script?
#!/bin/bash
echo 'Please enter your name'
read name
echo "My Name is $name"Q:21 How to unset a variable or its value? # unset <variable_name> Q:22 How to perform arithmetic operations?
Method 1: using expr: # expr 5 + 2 Method 2: using $[ expression ]: test=$[16 + 4] Q:23 What is the basic format of a do‑while loop?
do {
# commands
} while (condition)Q:24 How to define a function in a shell script?
diskusage () { df -h; }
# or
function func_name() {
# commands
return int
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
