Variables and Flow Control in Linux Shell Scripts
Linux Shell scripts are a handy automation tool that can help us accomplish various complex tasks. In this article, we'll take a detailed look at variables and flow control statements in shell scripts, and how to use them to write efficient, readable scripts.
variable
In shell scripts, variables are containers for storing and referencing data. Variable names consist of letters, numbers, and underscores, but cannot begin with a number.
define variable
In shell scripts, variables can be defined using the equal sign (=). There cannot be a space between the variable name and the equal sign.
name="John Doe"
age=30
reference variable
To reference a variable, precede the variable name with a dollar sign ($). When quoting variables, you can enclose the variable name in double quotes (") or single quotes ('). Variables inside double quotes are expanded, while those inside single quotes are not.
echo "My name is $name and I am $age years old."
echo 'My name is $name and I am $age years old.'
output:
My name is John Doe and I am 30 years old.
My name is $name and I am $age years old.
variable manipulation
- String concatenation:
string1="Hello, "
string2="world!"
combined_string="$string1$string2"
echo $combined_string
output:
Hello, world!
- String length:
string="Hello, world!"
length=${
#string}
echo "The length of the string is $length."
output:
The length of the string is 13.
- String interception:
string="Hello, world!"
substring=${string:7:5}
echo "The substring is '$substring'."
output:
The substring is 'world'.
flow control statement
Flow control statements are used to control the execution order of scripts. Common flow control statements include: if-then-else, case, for, while, until, etc.
if-then-else statement
if-then-else statements are used to execute different blocks of code based on conditions.
number=5
if [ $number -lt 10 ]; then
echo "The number is less than 10."
elif [ $number -eq 10 ]; then
echo "The number is equal to 10."
else
echo "The number is greater than 10."
fi
output:
The number is less than 10.
Note that test conditions need to be enclosed in square brackets ([ ]) with spaces between the square brackets.
case statement
A case statement is used to execute different blocks of code depending on the value of a variable.
fruit="apple"
case $fruit in
"apple")
echo "It's an apple."
;;
"banana")
echo "It's a banana."
;;
*)
echo "It's something else."
;;
esac
output:
It's an apple.
for loop
A for loop is used to repeatedly execute a block of code.
for i in {
1..5}; do
echo "Iteration $i"
done
output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
while loop
A while loop executes a section of code repeatedly while a condition is met.
i=1
while [ $i -le 5 ]; do
echo "Iteration $i"
i=$((i + 1))
done
output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
until loop
The until loop repeatedly executes a section of code when a condition is not met.
i=1
until [ $i -gt 5 ];do
echo "Iteration $i"
i=$((i + 1))
done
output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Summarize
This article provides a detailed introduction to variables and flow control statements in Linux Shell scripts, and how to use them to write efficient and readable scripts. By mastering these basic concepts, you will be able to use Shell scripts more skillfully to complete various tasks. Of course, the functions of Shell script are far more than these, and there are many advanced features waiting for you to explore and learn.