Understanding Linux Shell: Basics, Scripts, and Common Commands
This article introduces the Linux shell as the user‑kernel interface, explains why Bash is the most popular shell, and covers script execution methods, variable handling, operators, control structures, and custom functions with concrete command examples.
linux shell
The shell is an interface for users to interact with the kernel; the most widely used shell is Bash.
The shell is also an interpreted programming language, known as shell scripting.
A system can have multiple shells; you can list installed shells with cat /etc/shells . Different shells may support different command syntaxes.
Shell script execution methods
Run a 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 Prefix the script path with a dot to execute it in the current shell.
./helloworld.sh
. ./helloworld.shThe third method runs in the current shell, whereas the first two run in a subshell.
Variables in Linux
Categories
System variables
User‑defined variables
Common system variables include:
$PATH $PWD $USER $SHELLDisplay all current variables set The set command lists all variables.
Defining variables
Format: NAME=value Notes:
No spaces around the equals 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.
If the value contains whitespace, enclose it in single quotes.
To include special characters like $, use single quotes.
STR=helloworld
echo $STRUnsetting a variable
unset STR
echo $STRDefining a read‑only variable
readonly B=2
echo $BRead‑only variables cannot be unset.
Using export makes a variable global for other shell processes.
Assigning command output to a variable A=`ls -al` or A=$(ls -al) Special shell variables $? – exit status of the previous command. $$ – current process ID. $0 – name of the current script. $n – nth positional parameter (n ≥ 1). $# – number of positional parameters, often used in loops. $* and $@ – list of all parameters; quoted forms preserve spacing differently.
Operators
Arithmetic format: expr m + n or $((m+n)).
expr 1 + 1
echo `expr 1 + 1`Note: there must be spaces between numbers and operators.
echo $((1+1))if syntax
if condition
then
statements
[elif condition
then statements ...]
[else statements ]
fiif and fi must match; elif is used for else‑if.
Test expressions
First form
Syntax: [ condition ] Spaces are required around condition. Non‑empty strings evaluate to true; result 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.
Second form
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 loop
Syntax examples:
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 Second form using C‑style syntax:
for ((i=0;i<5;i++))
do
echo $i
doneor on one line:
for ((i=0;i<5;i++));do echo $i; donewhile loop
First form:
while expression
do
command
…
doneSecond form with a counter:
i=1
while ((i<=3))
do
echo $i
let i++
donecase syntax
Format:
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; function keyword and parentheses cannot both be omitted.
Calling a function:
function_name arguments function b() {
echo $1
}
b aA function must be defined before it is called because the shell interprets scripts line by line.
Function return values are accessed via $?. You can explicitly return a numeric value (0‑255); 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.
