Fundamentals 9 min read

Mastering Linux Shell: Basics, Scripts, Variables, and Control Structures

This article introduces the Linux shell as an interface and scripting language, explains how to execute scripts, manage system and user variables, use special variables, operators, conditional statements, loops, case constructs, and define functions, providing concrete syntax examples for each feature.

ZhiKe AI
ZhiKe AI
ZhiKe AI
Mastering Linux Shell: Basics, Scripts, Variables, and Control Structures

linux shell

Shell is the interface for users to interact with the kernel; the most popular shell is Bash.

Shell is also an interpreted programming language, i.e., shell scripts.

A system can have multiple shells; list installed shells with cat /etc/shells . Different shells may support different command syntax.

shell script execution methods

Run the script by its absolute or relative path after granting execute permission.

./helloworld.sh
/tmp/helloworld.sh

If the script lacks execute permission, run it with sh . sh helloworld.sh Prepend a dot to the script path.

./helloworld.sh
. ./helloworld.sh

The third method runs in the current shell, whereas the first two run in a subshell.

linux variables

Classification

System variables

User-defined variables

Common system variables:

$PATH
$PWD
$USER
$SHELL

Display all variables set The set command shows all variables in the system.

Define variables

Format: NAME=VALUE Notes

No spaces around the equal sign.

Variable names are conventionally uppercase.

Double quotes preserve spaces; single quotes preserve all special characters.

Variable names are case‑sensitive.

Names cannot start with a digit.

Enclose whitespace in single quotes.

Enclose special characters such as $ in single quotes.

STR=helloworld
echo $STR

Unset a variable

unset STR
echo $STR

Define a read‑only variable

readonly B=2
echo $B

Read‑only variables cannot be unset.

Use export VAR to promote a variable to the environment for other shell processes.

Assign command output to a variable A=`ls -al` or A=$(ls -al) Special shell variables $? – exit status of the previous command $$ – PID of the current shell $0 – name of the current script $n – nth positional parameter (n ≥ 1) $# – number of positional parameters $* and $@ – all positional parameters; behavior differs when quoted

Operators

Format: expr m + n or

$((m+n))
expr 1 + 1
echo `expr 1 + 1`

Spaces are required between numbers and operators.

echo $((1+1))

If syntax

if condition
then
    statements
[elif condition
    then statements ...]
[else statements]
fi

if and fi must appear as a pair; elif is the else‑if form.

Test statements

Form 1

Syntax: [ condition ] Spaces are required around condition. A non‑empty result returns true; the exit status can be checked with $? (0 = true, >0 = false).

[ condition ] && echo OK || echo notok
&&

returns true only if both sides are true; || returns true if either side is true.

Form 2

Syntax:

test condition

Common test operators

=

– string equality -lt – less than -le – less than or equal -eq – equal -gt – greater than -ge – greater than or equal -ne – not equal -r – readable -w – writable -x – executable -f – regular file exists -s – file exists and is not empty -d – directory exists -b – block device exists -L – symbolic link exists

for loops

Two styles:

for N in 1 2 3
do
  echo $N
done

or for N in 1 2 3; do echo $N; done or

for N in {1..2}; do echo $N; done
for ((i=0;i<5;i++))
do
  echo $i
done

or

for ((i=0;i<5;i++)); do echo $i; done

while loops

Form 1:

while expression
do
  command
  …
 done

Form 2:

i=1
while ((i<=3))
do
  echo $i
  let i++
done

case syntax

case $1 in
start)
  echo "starting"
  ;;
stop)
  echo "stoping"
  ;;
*)
  echo "Usage: {start|stop}"
esac
;;

works like break in other languages; * is the default case.

Shell custom functions

Syntax:

[ function ] funname [()]
{
    action;
    [return int;]
}

Square brackets indicate optional parts; the function keyword and parentheses cannot both be omitted.

Calling a function:

function_name arguments
function b() {
  echo $1
}
b a

Functions must be defined before they are called because the shell interprets line by line.

Function return values are obtained via $?. An explicit return n (0‑255) can be used; otherwise the exit status of the last command is used.

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.

LinuxShellcommand-lineBashVariablesShell scriptingControl structures
ZhiKe AI
Written by

ZhiKe AI

We dissect AI-era technologies, tools, and trends with a hardcore perspective. Focused on large models, agents, MCP, function calling, and hands‑on AI development. No fluff, no hype—only actionable insights, source code, and practical ideas. Get a daily dose of intelligence to simplify tech and make efficiency tangible.

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.