Master Shell Scripting: From Basics to Advanced Control Structures
This guide explains what a shell script file is, how to write Bash scripts with proper syntax, manage variables, use redirection, pipes, quoting, grep, operators, arrays, control flow statements, functions, and even automate interactions with an Expect script, providing clear examples for each concept.
What is a script file?
A script is a plain‑text file interpreted by a command processor. When executed, the commands inside run sequentially. Frequently used Linux commands can be stored in a file (e.g., run.sh) and executed as a script.
#!/bin/bash
ls
pwd
cd ..
touch hello.cWriting shell scripts
Basic rules
File extension should be .sh.
The first line must be #!/bin/bash to specify the Bash interpreter.
Lines beginning with # are comments.
Use echo to output text.
Use cat to view file contents.
Shell variables
All variables are treated as strings, even numeric values.
Creating variables
#!/bin/bash
a=10
b=" 10"
readonly c=3 # read‑only variableReferencing variables
#!/bin/bash
a=10
echo $a
echo ${a}
echo "${a}"The script prints three lines, each containing 10.
Deleting variables
#!/bin/bash
a=10
unset a
echo $a # nothing is printed because the variable was removedReading variables from the keyboard
#!/bin/bash
echo "please input the first number:"
read a
echo "This number is: $a"Variable types
Special variables
Example a.sh prints common positional and status variables:
#!/bin/bash
echo $1 # first argument
echo $2 # second argument
echo $3 # third argument
echo $0 # script name
echo $# # number of arguments
echo $@ # all arguments as separate words
echo $* # all arguments as a single word
echo $? # exit status of last command
echo $$ # PID of the script
echo "finally"System / environment variables
Predefined variables such as HOME, PWD, PATH, USER can be listed with env. To make a variable available to child processes, export it:
export VAR_NAMEFor persistence, add the export line to ~/.bashrc and reload with source ~/.bashrc.
Redirection operators
When the target file does not exist, redirection creates it.
#!/bin/bash
echo hello > 1.c # write to 1.c
cat < 1.c > 2.c # copy content to 2.c
cat 2.c # display 2.cPipe ( | )
The pipe connects the standard output of the left command to the standard input of the right command.
Single vs double quotes
Single quotes suppress all special meaning; double quotes allow variable expansion ( $) and command substitution.
#!/bin/bash
a=10
echo ${a} # 10
echo "${a}" # 10
echo '${a}' # $a (no expansion)grep search options
-i : ignore case
-r : recursive search
-l : print only matching file names
-n : print line numbers
-v : invert match (show non‑matching lines)
-w : match whole words only
-c : print count of matching lines
Example file 1.txt:
"hello world"
"this is a test"
12Command:
grep "hello" 1.txtTest operators
#!/bin/bash
VAR=2
test $VAR -gt 1
echo $? # 0 if true, 1 if false
VAR1=3
[ $VAR1 -gt 1 ] # spaces required around brackets
echo $?Arrays
Definition methods:
a=(1 2 3 4 5) a[0]=1; a[1]=2; a[2]=3 a=([1]=1 [2]=2)Example:
#!/bin/bash
a=(2 5 7 10)
echo ${a[2]} # element at index 2
echo ${#a[*]} # array length
echo ${a[@]:2} # slice from index 2 to end
echo ${a[@]:1:2} # two elements starting at index 1If statements
Place if and then on separate lines, or terminate the condition with ; when they share a line.
#!/bin/bash
# style 1
if [ $USER == "self" ]
then
echo $USER
fi
# style 2 (single line)
if [ $PWD == "/home/self/" ]; then echo $PWD; fi
# style 3 with elif / else
if [ $PWD == "/home/self/" ]; then
echo "HOME $PWD"
elif [ $PWD == "/mnt/hgfs/share/5.shell/3" ]; then
echo "SHARE $PWD"
else
echo "else"
fiCase statements
#!/bin/bash
case $1 in
"y") echo inputed y ;;
"n") echo inputed n ;;
*) echo "inputed *" ;;
esacFor loops
#!/bin/bash
# iterate over a list
for i in 1 2 3 4 5; do
echo $i
done
# C‑style loop
for ((i=0;i<5;i++)); do
echo $i
done
# iterate over files in a directory
for i in /etc/*; do
echo $i
doneWhile loops
#!/bin/bash
var=0
while [ $var -ne 10 ]; do
echo $var
var=$((var+1))
doneUntil loops
#!/bin/bash
myvar=0
until [ $myvar -eq 10 ]; do
echo $myvar
myvar=$((myvar+1))
doneShell functions
#!/bin/bash
func() {
echo "hello world"
echo $0 # script name
echo $1 # first argument
return 255
}
func 12 33
exit 0
echo $?Writing an Expect script for automated interaction
Install the Expect interpreter (Debian/Ubuntu):
sudo apt-get install expectExample test.exp automates an scp transfer by providing the password and handling the host‑key prompt.
#!/usr/bin/expect
# Set variables
set user "HwHiAiUser"
set host "192.168.21.8"
set password "Mind@123"
# Start scp
spawn scp test $user@$host:~
# Handle prompts
expect {
"password:" {
send "${password}\r"
}
"yes/no" {
send "yes\r"
expect "password:" { send "${password}\r" }
}
}
# Wait for scp to finish
expect eofSigned-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.
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.)
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.
