Master Bash: From Basic Scripts to Advanced Automation with Expect
This guide explains what script files are and walks through writing Bash scripts, covering naming conventions, variables, special and environment variables, redirection, pipes, quoting, grep options, operators, arrays, control structures, functions, and automating interactions with Expect, providing practical code examples for each concept.
What is a script file?
A script file is a plain‑text file that contains a sequence of commands interpreted by a command parser (e.g., Bash). When executed, the commands run in order, allowing frequently used Linux commands to be stored and reused.
#!/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 interpreter.
Comments start with #.
Use echo to output text.
Use cat to view file contents.
Shell Variables
Creating variables
Variable assignment uses the syntax name=value with no spaces around the equals sign. Values containing spaces must be quoted.
#!/bin/bash
a=10
b=" 10"
readonly c=3 # read‑only variableReferencing variables
Access a variable with $name. The three forms below are equivalent:
#!/bin/bash
a=10
echo $a
echo ${a}
echo "${a}"Deleting variables
Remove a variable with unset name.
#!/bin/bash
a=10
unset a
echo $a # no outputReading input from the keyboard
Prompt the user with read var.
#!/bin/bash
echo "please input the first number:"
read a
echo "This number is: $a"Special Variables
#!/bin/bash
echo $1 # first positional argument
echo $2
echo $3
echo $0 # script name
echo $# # number of arguments
echo $@
echo $*
echo $?
echo $$Environment Variables
Predefined variables such as HOME, PWD, USER, HOSTNAME, etc., can be listed with env. Export a variable with export VAR to make it available to child processes, and add it to ~/.bashrc for persistence.
Redirection Operators
When the target file does not exist, redirection creates it automatically.
#!/bin/bash
echo hello > 1.c # write to 1.c
cat < 1.c > 2.c # copy contents
cat 2.c # displayPipe Operator
The pipe | passes the standard output of the command on the left as standard input to the command on the right.
Single vs Double Quotes
Single quotes '...' suppress all special meanings. Double quotes "..." preserve variable expansion ( $) and command substitution.
#!/bin/bash
a=10
echo ${a} # 10
echo "${a}" # 10
echo '${a}' # $agrep Search Options
Typical usage: grep [options] pattern file. Common options: -i – ignore case -r – recursive search -l – list matching file names -n – show line numbers -v – invert match -w – match whole words -c – count matching lines
Example file 1.txt:
"hello world"
"this is a test"
12Command grep "hello" 1.txt prints the line containing "hello".
Test Operators
#!/bin/bash
VAR=2
test $VAR -gt 1
echo $?
[ $VAR -gt 1 ] # spaces required around brackets
echo $?Arrays
Defining arrays:
a=(1 2 3 4 5) a[0]=1; a[1]=2; a[2]=3 a=([1]=1 [2]=2)Accessing arrays:
#!/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 1Control Structures
if Statement
Place if and then on separate lines, or terminate the condition with a semicolon when on the same 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/share/" ]; then
echo "SHARE $PWD"
else
echo "else"
ficase Statement
#!/bin/bash
case $1 in
"y") echo inputed y ;;
"n") echo inputed n ;;
*) echo "inputed *" ;;
esacfor Loop
#!/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
for f in /etc/*; do
echo $f
donewhile Loop
#!/bin/bash
var=0
while [ $var -ne 10 ]; do
echo $var
var=$((var+1))
doneuntil Loop
#!/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 $?Automating Interaction with expect
Install the Expect interpreter: sudo apt-get install expect The following Expect script 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.
