Top 24 Shell Script Interview Questions and Answers Every Linux Engineer Should Know
This article compiles the most frequently asked shell script interview questions, providing clear explanations, code examples, and practical tips on topics such as script basics, variables, redirection, control structures, debugging, and common commands for Linux operations.
Although Python is widely used in operations, many companies still ask shell script questions when hiring Linux cloud engineers. Below are common shell script interview questions and answers.
Q1: What is a shell script and is it necessary?
A shell script is a text file containing one or more commands. System administrators often combine multiple commands into a single script to automate routine tasks.
Q2: What is the default login shell and how to change a user's login shell?
The default login shell is /bin/bash. Use chsh to change it, e.g.:
# chsh <username> -s <new_shell>
# chsh linuxtechi -s /bin/shQ3: What types of variables can be used in shell scripts?
System-defined variables (usually uppercase, viewable with set)
User-defined variables (accessed with $VAR_NAME)
Q4: How to redirect both standard output and error to the same location?
Method 1: 2>&1 # ls /usr/share/doc > out.txt 2>&1 Method 2: &> # ls /usr/share/doc &> out.txt Q5: How to nest "if" statements in a shell script?
if [ condition ]
then
# commands
else
if [ condition ]
then
# commands
else
# commands
fi
fiQ6: What is the purpose of the $? variable? $? holds the exit status of the previous command; 0 means success.
# ls /usr/bin/shar
# echo $?
0Q7: 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"
fiQ8: What does the break command do?
It exits the current loop (e.g., while or until).
Q9: What does the continue command do?
It skips the remainder of the current loop iteration and proceeds with the next iteration.
Q10: What is the syntax of a case statement?
case variable in
value1)
# commands
;;
value2)
# commands
;;
esacQ11: What is the syntax of a while loop?
while [ condition ]
do
# commands
doneQ12: How to make a script executable? # chmod a+x myscript.sh Q13: What is the purpose of #!/bin/bash ?
It is the shebang line that tells the system to execute the script using /bin/bash.
Q14: What is the syntax of a for loop?
for variable in list
do
# commands
doneQ15: How to debug a shell script?
Use the -x option ( sh -x myscript.sh) or -nv ( sh -nv myscript.sh).
Q16: How to compare strings in a shell script?
Use the test command or [ ] with string operators.
Q17: What special variables does Bourne shell (bash) provide?
$0 script name
$1 first argument
$2 second argument
...
$# number of arguments
$* all arguments as a single wordQ18: How to test files in a shell script?
-d file # true if directory
-e file # true if exists
-f file # true if regular file
-r file # true if readable
-s file # true if non‑empty
-w file # true if writable
-x file # true if executableQ19: How to write comments in a shell script? # This is a comment Q20: How to read input from the terminal?
#!/bin/bash
echo "Please enter your name"
read name
echo "My Name is $name"Q21: How to unset a variable? # unset <variable_name> Q22: How to perform arithmetic operations?
Method 1: expr 5 + 2 Method 2: result=$[16 + 4] Q23: What is the basic format of a do‑while loop?
do {
# commands
} while (condition)Q24: How to define a function in a shell script?
myfunc () {
# commands
}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.
