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.
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.shIf the script lacks execute permission, run it with sh . sh helloworld.sh Prepend a dot to the script path.
./helloworld.sh
. ./helloworld.shThe 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 $SHELLDisplay 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 $STRUnset a variable
unset STR
echo $STRDefine a read‑only variable
readonly B=2
echo $BRead‑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]
fiif 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 conditionCommon 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
doneor 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
doneor
for ((i=0;i<5;i++)); do echo $i; donewhile loops
Form 1:
while expression
do
command
…
doneForm 2:
i=1
while ((i<=3))
do
echo $i
let i++
donecase 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 aFunctions 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
