Operations 10 min read

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

This article compiles the most frequently asked shell script interview questions, providing clear explanations, essential concepts, and practical code examples to help Linux and cloud engineers master scripting for automation, debugging, and system administration tasks.

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

Shell scripting is a fundamental skill for Linux and cloud engineers, and interviewers often test candidates on it. Below are 24 common interview questions with concise answers and code examples.

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

A shell script is a text file containing one or more commands. It allows system administrators to bundle multiple commands into a single file to automate routine tasks.

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

The default login shell on most Linux systems is /bin/bash. Use chsh 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 of variables are used:

System-defined variables (usually uppercase, viewable with set).

User-defined variables (accessed with $variable_name).

Q4: How to redirect both standard output and error output to the same location?

Two methods:

# ls /usr/share/doc > out.txt 2>&1
# 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
fi

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

"$?" holds the exit status of the previous command; 0 means success, any other value indicates failure.

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?

break

exits the innermost loop (e.g., while or until).

Q9: What does the continue command do?

continue

skips the current iteration of a loop and proceeds with the next one, useful for handling errors without aborting the whole loop.

Q10: What is the syntax of a case statement?

case variable in
    value1)
        # commands
        ;;
    value2)
        # commands
        ;;
    *)
        # default commands
        ;;
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"?

This is the shebang line; it 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
 done

Q15: How to debug a shell script?

Run the script with sh -x myscript.sh or sh -nv myscript.sh to trace execution.

Q16: How to compare strings in a shell script?

Use the test command or [ "${str1}" = "${str2}" ] syntax.

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

$0

– script name $1$9 – positional parameters $# – number of arguments $* – all arguments as a single word

Q18: 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 not empty -w file – true if writable -x file – true if executable

Q19: 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?

Two methods:

# expr 5 + 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() {
    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.

AutomationinterviewSysadminBashShell scripting
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.