Operations 12 min read

Master Shell Scripting: 25 Essential Q&A for Linux Administrators

This article presents 25 concise questions and answers covering fundamental shell scripting concepts—including script creation, variables, redirection, control structures, debugging, and common commands—providing Linux administrators with a quick reference guide to write and manage effective Bash scripts.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Shell Scripting: 25 Essential Q&A for Linux Administrators

Shell Scripting Basics – Q&A

Q: What is a Shell script and is it necessary?

A Shell script is a plain‑text file that contains one or more commands. System administrators often need to run several commands to complete a task, so they place those commands in a script to automate routine work.

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

The default login shell on most Linux systems is /bin/bash. It can be changed with the chsh command, e.g.:

# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh
Q: What types of variables can be used in a Shell script?

Two kinds of variables are available:

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

User‑defined variables (created by the user; their values can be displayed with echo $<var_name>).

Q: How can standard output and error output be redirected to the same location?

Two common methods:

2>&1   # ls /usr/share/doc > out.txt 2>&1
&>    # ls /usr/share/doc > out.txt
Q: How is the if syntax nested in a Shell script?

Basic nested if example:

if [ Condition ]
then
    command1
    command2
    ...
else
    if [ condition ]
    then
        command1
        command2
        ...
    else
        command1
        command2
        ...
    fi
fi
Q: What is the purpose of the $? variable?
$?

holds the exit status of the previously executed command; 0 indicates success, any non‑zero value indicates failure. Example:

# 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
Q: How to compare two numbers in a Shell script?

Use test operators such as -gt:

#!/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
Q: What does the break command do?
break

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

Q: What does the continue command do?
continue

skips the remainder of the current loop iteration and proceeds with the next iteration.

Q: What is the syntax of a case statement?

Basic syntax:

case word in
value1)
    command1
    command2
    ...
    ;;
value2)
    command1
    command2
    ...
    ;;
esac
Q: What is the syntax of a while loop?

Basic syntax:

while [ test_condition ]
do
    commands…
done
Q: How to make a script executable?

Use chmod:

# chmod a+x myscript.sh
Q: What does #!/bin/bash mean?

It is the shebang line that tells the system to execute the script with /bin/bash.

Q: What is the syntax of a for loop?

Basic syntax:

for variable in list_of_items
do
    command1
    command2
    ...
    done
Q: How to debug a Shell script?

Run the script with the -x option ( sh -x myscript.sh) or with -nv for a dry‑run.

Q: How to compare strings in a Shell script?

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

Q: What special variables exist in the Bourne shell (bash)?

Built‑in Variable

Explanation

$0

Script name

$1

First positional parameter

$2

Second positional parameter

$9

Ninth positional parameter

$#

Number of positional parameters

$*

All positional parameters as a single word

Q: How to test files in a Shell script?

Use the test command with various operators: Test Usage -d filename True if the file exists and is a directory -e filename True if the file exists -f filename True if the file exists and is a regular file -r filename True if the file exists and is readable -s filename True if the file exists and is not empty -w filename True if the file exists and is writable -x filename True if the file exists and is executable

Q: How to write comments in a Shell script?

Comments start with #. Example:

#!/bin/bash
# This is a comment
 echo "I am logged in as $USER"
Q: How to read input from the terminal?

Use the read command:

#!/bin/bash
echo "Please enter your name"
read name
echo "My Name is $name"
Q: How to unset a variable or its value?

Use unset <variable_name>.

# unset VAR_NAME
Q: How to perform arithmetic operations?

Two methods:

Using expr (e.g., expr 5 + 2).

Using the arithmetic expansion syntax $[ expression ] (e.g., result=$[16 + 4]).

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

Example:

do
{
    statements
} while (condition)
Q: How to define a function in a Shell script?

Define a named code block and call it by name:

diskusage () { df -h ; }
Q: How to use bc (Bash calculator) inside a script?

Example:

variable=`echo "scale=2; 5/2" | bc`
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.

sysadminloopsRedirectionshell-variables
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.