Fundamentals 12 min read

Master the Classic Shell Interview Questions: 13 Essential Concepts Explained

This article presents the classic "Shell Thirteen Questions" covering the definition of a shell, prompt mechanics, echo behavior, quoting rules, variable export, exec versus source, command grouping, substitution, wildcard handling, logical operators, I/O redirection, conditional statements, and loop constructs, each with clear explanations and examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master the Classic Shell Interview Questions: 13 Essential Concepts Explained

1. Why is it called "shell"?

A shell is the user‑level interface that sits on top of the operating system kernel, translating user commands into kernel actions. It acts as a command interpreter, allowing users to interact with the system via a command line.

Translates user commands to the kernel.

Translates kernel responses back to the user.

2. How does the Shell prompt (PS1) relate to Carriage Return (CR)?

After logging into a shell, the cursor left of the prompt indicates where you can type commands. The prompt (e.g., $ for normal users, # for root) appears before a Carriage Return character, which signals the end of the command line and triggers execution.

Shell prompt: ready for input until a CR is received.

Carriage Return: indicates the command can be executed.

3. What does echo actually do?

echo sends its arguments to standard output (stdout), typically displaying them on the screen.

stdin – standard input

stdout – standard output

stderr – standard error

echo -n   # suppress newline
echo -e   # enable backslash escape

4. Difference between double quotes (") and single quotes (')

''

(single quotes) – hard quoting; disables all variable and command expansion. "" (double quotes) – soft quoting; allows variable expansion ( $var) while preserving spaces.

5. What changes when you add export to a variable assignment?

Assigning name=value creates a shell variable. Adding export ( export name=value ) promotes it to an environment variable, making it visible to child processes.
# Local variable
A=B
unset A
# Environment variable
export A=B

6. How do exec and source differ?

Environment variables flow only from parent to child. source runs a script in the current shell, allowing it to modify the current environment. exec replaces the current shell process with the script, so changes do not affect the original shell.
# Run script in a subshell
./script.sh
# Run script in the current shell
source script.sh
# Replace current shell with script
exec script.sh

7. Difference between ( ) and { } ?

( )

creates a subshell to execute a command group, isolating variable changes. { } groups commands in the current shell, so any variable modifications persist.

8. How do $() , backticks, and ${} differ?

$()

and backticks perform command substitution. ${var} and $var perform variable substitution.

# Example variable
file=/dir1/dir2/dir3/my.file.txt
# Remove leading path components
${file#*/}   # dir1/dir2/dir3/my.file.txt
${file##*/}  # my.file.txt
# Remove file extension
${file%.*}  # /dir1/dir2/dir3/my.file

9. Difference between "$@" and "$*" ?

"$@"

expands to separate quoted arguments (e.g., "p1" "p2 p3" "p4"). "$*" expands to a single quoted string containing all arguments (e.g., "p1 p2 p3 p4").

10. How do && and || differ?

cmd1 && cmd2

executes cmd2 only if cmd1 succeeds (returns 0). cmd1 || cmd2 executes cmd2 only if cmd1 fails (non‑zero return).

11. What do > and < do?

< redirects a file to standard input. > redirects standard output (or standard error with 2> ) to a file.
# Redirect stdout and stderr
ls my.file no.such.file 1>file.out 2>file.err
# Merge stderr into stdout
ls my.file no.such.file 1>file.out 2>&1
# Discard output
ls my.file no.such.file > /dev/null 2>&1

12. When to use if versus case ?

# if example
read YN
if [ "$YN" = Y ] || [ "$YN" = y ] || [ "$YN" = Yes ]; then
    echo "continue"
else
    exit 0
fi

# case example
read YN
case "$YN" in
    [Yy]|[Yy][Ee][Ss]) echo "continue";;
    *) exit 0;;
esac

13. Differences among for , while , and until loops

# for loop
for ((i=1;i<=10;i++)); do
    echo "num is $i"
done

# while loop
num=1
while [ "$num" -le 10 ]; do
    echo "num is $num"
    num=$((num+1))
 done

# until loop
num=1
until [ "$num" -gt 10 ]; do
    echo "num is $num"
    num=$((num+1))
 done
break

exits a loop. return exits a function. exit terminates the script or shell.

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.

LinuxShellScriptingBashcommand-line
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.