Top 24 Shell Script Interview Questions Every Engineer Should Master
This article compiles the most common shell script interview questions—from basic concepts and variable types to redirection, control structures, debugging, and special Bash variables—providing concise answers and code examples to help candidates prepare effectively.
Shell scripting is a fundamental skill for Linux operations, and interviewers often test candidates with a range of practical questions.
Q1: What is a shell script and is it necessary?
A shell script is a text file that contains one or more commands. System administrators frequently combine multiple commands into a single script to automate routine tasks.
Q2: What is the default login shell and how can you change a user's login shell?
In Linux, /bin/bash is the default login shell assigned when a user is created. You can change a user's shell with the chsh command.
# chsh <username> -s <new_shell> # chsh linuxtechi -s /bin/sh
Q3: What types of variables can be used in a shell script?
Shell scripts support two kinds of variables:
System-defined variables (usually uppercase, viewable with set)
User-defined variables (accessed with $VARIABLE_NAME)
Q4: How can you redirect both standard output and error output to the same location?
Two common methods:
2>&1 # e.g., ls /usr/share/doc > out.txt 2>&1
&> # e.g., ls /usr/share/doc &> out.txt
Q5: How do you nest "if" statements in a shell script?
Basic syntax:
if [ condition ] then command1 command2 else if [ condition ] then command1 command2 else command1 command2 fi fi
Q6: What does the "$?" variable indicate?
It holds the exit status of the previously executed command; 0 means success, any non‑zero value indicates failure.
root@localhost:~# ls /usr/bin/shar /usr/bin/shar root@localhost:~# echo $? 0
root@localhost:~# ls /usr/bin/share ls: cannot access /usr/bin/share: No such file or directory root@localhost:~# echo $? 2
Q7: How do you compare two numbers in a shell script?
Use test operators such as -gt inside an if statement.
#!/bin/bash x=10 y=20 if [ $x -gt $y ] then echo "x is greater than y" else echo "y is greater than x" fi
Q8: What does the break command do?
It exits the innermost 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) command1 command2 ... ;; value2) command1 command2 ... ;; esac
Q11: What is the syntax of a while loop?
while [ condition ] do commands … done
Q12: How do you 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 command1 command2 ... done
Q15: How can you debug a shell script?
Use the -x option ( sh -x myscript.sh) or the -nv option ( sh -nv myscript.sh).
Q16: How do you compare strings in a shell script?
Use the test command (or [ ]) to compare strings character by character.
Q17: What special variables does the Bourne shell (bash) provide?
Examples include: $0 – script name $1 – first argument
… up to
$9 $#– number of arguments $* – all arguments as a single word
Q18: How do you test files in a shell script?
Use the test command with flags such as: -d filename – true if it exists and is a directory -e filename – true if it exists -f filename – true if it exists and is a regular file -r filename – true if readable -s filename – true if not empty -w filename – true if writable -x filename – true if executable
Q19: How do you write comments in a shell script?
#!/bin/bash # This is a comment echo "I am logged in as $USER"
Q20: How can a script read input from the terminal?
# vi /tmp/test.sh #!/bin/bash echo 'Please enter your name' read name echo "My Name is $name" # ./test.sh Please enter your name LinuxTechi My Name is LinuxTechi
Q21: How do you unset a variable or its value?
# unset <variable_name>
Q22: How can you perform arithmetic operations?
Two methods:
Using expr (e.g., expr 5 + 2)
Using the $[ expression ] syntax (e.g., test=$[16 + 4])
Q23: What is the basic format of a do‑while loop?
do { commands } while (condition)
Q24: How do you define a function in a shell script?
function_name() { commands; }
These questions and answers provide a solid foundation for anyone preparing for a Linux shell scripting interview.
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.
