Operations 11 min read

Top 23 Shell Script Interview Questions and Answers Every Engineer Should Know

This article compiles the most common shell script interview questions, providing clear explanations of script basics, variable types, redirection, control structures, special variables, file testing, arithmetic, functions, and debugging techniques to help candidates master Linux automation.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Top 23 Shell Script Interview Questions and Answers Every Engineer Should Know

Shell scripting is a core skill for Linux system automation and is frequently tested in technical interviews.

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

A shell script is a text file containing one or more commands; it lets administrators bundle multiple commands into a single executable file to automate routine tasks.

Q2: What is the default login shell and how can it be changed for a specific user?

In most Linux distributions the default login shell is /bin/bash. The chsh command changes a user’s login shell, e.g.:

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

Q3: What types of variables can be used in a shell script?

System‑defined variables (usually uppercase, viewable with set)

User‑defined variables (accessed with $VAR_NAME)

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

Two common methods:

Append 2>&1 to the command, e.g. ls /usr/share/doc > out.txt 2>&1 Use &> shorthand, e.g.

ls /usr/share/doc &> out.txt

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

if [ condition ]
then
    # commands
    if [ inner_condition ]
    then
        # inner commands
    else
        # else inner
    fi
else
    # else outer
fi

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

$?

holds the exit status of the most recently executed command; 0 indicates success, any non‑zero value indicates failure.

Q7: How to compare two numbers in a shell script?

Use test operators such as -gt, -lt, -eq inside an if statement:

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 remainder of the current loop iteration and proceeds with the next iteration.

Q10: What is the syntax of a case statement?

case $var in
    pattern1)
        # commands
        ;;
    pattern2)
        # commands
        ;;
    *)
        # default
        ;;
esac

Q11: What is the syntax of a while loop?

while [ condition ]
do
    # commands
    # ...
done

Q12: How to make a script executable?

Use chmod a+x script.sh to add execute permission.

Q13: What is the purpose of the #!/bin/bash shebang line?

The shebang tells the kernel to run the script with the /bin/bash interpreter.

Q14: What is the syntax of a for loop?

for var in list
do
    # commands
    # ...
done

Q15: How to debug a shell script?

Run the script with sh -x script.sh for trace output, or sh -nv script.sh to check syntax without execution.

Q16: How to compare strings in a shell script?

Use the test command or [ ] with operators like = and !=.

Q17: What special variables does Bash provide?

Examples include $0 (script name), $1$9 (positional parameters), $# (argument count), and $* (all arguments as a single word).

Q18: How to test file properties?

test -d file   # true if directory
test -e file   # true if exists
test -f file   # true if regular file
test -r file   # true if readable
test -w file   # true if writable
test -x file   # true if executable

Q19: How to write comments in a shell script?

Any line beginning with # 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:

Using expr, e.g. expr 5 + 2 Using 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() {
    # commands
    return 0
}

Functions allow reusable code blocks that can be invoked by name.

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.

automationinterviewBashShell scripting
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.