Operations 10 min read

Top 24 Shell Scripting Interview Questions Every Linux Engineer Should Know

This article compiles the most common shell scripting interview questions, covering fundamentals such as script basics, variables, redirection, control structures, special parameters, file tests, debugging techniques, and practical code examples to help Linux engineers prepare effectively.

ITPUB
ITPUB
ITPUB
Top 24 Shell Scripting Interview Questions Every Linux Engineer Should Know

Shell Scripting Interview Q&A

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

A shell script is a plain‑text file containing one or more commands that can be executed sequentially, allowing system administrators 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 the chsh command to change it, 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 to the same location?

Method 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
    command1
    command2
else
    if [ condition ]
    then
        command1
        command2
    else
        command1
        command2
    fi
fi

Q6: What does the $? variable represent?

$?

holds the exit status of the most recently executed command; 0 means success, any non‑zero 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 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 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 run the script using the /bin/bash interpreter.

Q14: What is the syntax of a for loop?

for variable in list
do
    command1
    command2
    …
    last_command
 done

Q15: How to debug a shell script?

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

Q16: How to compare strings?

Use the test command (or [ … ]) with string operators such as = and !=.

Q17: Common special variables in Bourne shell (bash)

$0

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

Q18: How to test file attributes with test ?

-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 executable

Q19: How to write comments in a 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?

Method 1 – expr: # expr 5 + 2 Method 2 – arithmetic expansion:

result=$[16 + 4]

Q23: What is the basic format of a do‑while loop?

do {
    commands
} while (condition)

Q24: How to define a function?

myfunc () {
    df -h
}

These questions and answers provide a solid foundation for anyone preparing for Linux shell scripting interviews.

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.

linuxSysadmininterview-questionsBashShell scripting
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.