Operations 11 min read

Top 24 Shell Script Interview Questions & Answers Every Linux Engineer Should Know

This article compiles the most common shell script interview questions, covering basics, variables, redirection, control structures, debugging, special variables, file tests, and execution permissions, providing concise answers and code examples to help Linux professionals prepare effectively.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Top 24 Shell Script Interview Questions & Answers Every Linux Engineer Should Know

Although Python is widely used in operations, many companies still ask shell script questions when hiring Linux cloud engineers because shell scripts automate many tasks.

Q1: What is a shell script and is it necessary?

A shell script is a text file containing one or more commands; administrators combine multiple commands into a script to automate routine tasks.

Q2: What is the default login shell and how to change a user's login shell?

In Linux, /bin/bash is the default login shell. Use the chsh command to change it.

# chsh <username> -s <new_shell>
# chsh linuxtechi -s /bin/sh

Q3: 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_name).

Q4: How to redirect both standard output and error output 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 &>)

Q5: How to nest "if" statements in a shell script?

if [ condition ]
then
    command1
    command2
    ...
else
    if [ condition ]
    then
        command1
        command2
        ...
    else
        command1
        command2
        ...
    fi
fi

Q6: What is the purpose of the $? variable?

It holds the exit status of the previous command; 0 means success, non‑zero indicates failure.

# ls /usr/bin/shar
/usr/bin/shar
# echo $?
0
# ls /usr/bin/share
ls: cannot access /usr/bin/share: No such file or directory
# echo $?
2

Q7: 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"
fi

Q8: 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 rest 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
        ;;
    *)
        default_command
        ;;
esac

Q11: What is the syntax of a while loop?

while [ condition ]
do
    commands...
done

Q12: 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
    command1
    command2
    ...
    final_command
done

Q15: How to debug a shell script?

Use the -x option ( sh -x myscript.sh) or the -nv option ( sh -nv myscript.sh).

Q16: How to compare strings in a shell script?

Use the test command (e.g., if [ "$str1" = "$str2" ]).

Q17: What are some special variables in the Bourne shell (bash)?

Examples include $0 (script name), $1$9 (positional parameters), $# (number of arguments), $* (all arguments), etc.

Q18: How to test files in a shell script?

Use the test command with options such as -d (directory), -e (exists), -f (regular file), -r (readable), -s (non‑empty), -w (writable), -x (executable).

Q19: How to write comments in a shell script?

#!/bin/bash
# This is a comment
echo "I am logged in as $USER"

Q20: 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"

Q21: How to unset a variable or its value? # unset <variable_name> Q22: How to perform arithmetic operations?

Use expr (e.g., expr 5 + 2) or the $[ expression ] syntax (e.g., 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() {
    df -h
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBash
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.