Shell Scripting Tutorial [1] - Shell Variables
Directory : https://blog.csdn.net/shn111/article/details/131590488
Reference tutorial : https://www.runoob.com/linux/linux-shell.html
Online editor : https://www.runoob.com/try/runcode.php?filename=helloworld&type=bash
Shell variables
Variable definitions
name="shn"
Note: There can be no spaces between the variable name and the equal sign
- Variable names can only use English letters, numbers and underscores, and the first character cannot start with a number
- Keywords in bash cannot be used (use the help command to view reserved keywords)
In addition to explicit direct assignment, you can also use statements to assign values to variables
name=$(ls)
name=`ls`
variable use
To use a defined variable, just add the dollar sign $ before the variable name
name="shn"
echo $name
echo ${name}
The curly braces outside the variable name are optional here. The curly braces are added to distinguish the boundaries of variables. It is recommended to add curly braces to all variables used
echo "I am ${name}hahaha"
If the above situation is written without curly braces, echo "I am $namehahaha"
the variable name cannot be distinguished
Defined variables can be redefined, no need to add $ sign when redefining, only needed when using variables
your_name="tom"
echo $your_name
your_name="alibaba"
echo $your_name
read-only variable
readonly
Variables can be defined as read-only variables using the command
The value of a read-only variable cannot be changed, and an error will be reported when executing the following script
your_name="tom"
readonly your_name
your_name="alibaba"
# script.sh: line 3: your_name: readonly variable
delete variable
unset
Variables can be deleted using the command
After a variable is deleted it cannot be used again. The unset command cannot delete read-only variables.
Executing the following command will not produce any output
your_name="tom"
unset your_name
echo ${your_name}
shell string
String representation can use single quotes, double quotes, or no quotes
apostrophe
name='bob'
str='this is a string'
echo ${str}
# this is a string
str2='hello ${name}'
echo ${str2}
# hello ${name}
str3='this is a \'string'
echo ${str3}
# unexpected EOF while looking for matching `''
str4='this is ''a string'
echo ${str4}
# this is a string
- Any characters in single quotes will be output as they are, and variables in single quote strings are invalid
- A single single quote cannot appear in a single quote string (even if it is escaped), but it can appear in pairs and used as a string concatenation
- To include single quotes enclose it inside a double-quoted string
Double quotes
name="bob"
str="this is a string"
echo ${str}
# this is a string
str2="hello ${name}"
echo ${str2}
# hello bob
str3="this is a \"string"
echo ${str3}
# this is a "string
str4="this is ""a string"
echo ${str4}
# this is a string
str5="this is a\tstring"
echo -e ${str5}
# this is a string
- There can be variables in double quotes
- Escape characters can appear in double quotes
- echo -e interprets escape characters such as \t \a \b \n
concatenate string
your_name="bob"
# 使用双引号拼接
greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"
echo $greeting $greeting_1
# hello, bob ! hello, bob !
# 使用单引号拼接
greeting_2='hello, '$your_name' !'
greeting_3='hello, ${your_name} !'
echo $greeting_2 $greeting_3
# hello, bob ! hello, ${your_name} !
get string length
use #get
When the variable is a string, ${#string} is equivalent to ${#string[0]}
string="abcd"
echo ${
#string}
# 4
string="abcd"
echo ${
#string[0]}
# 4
extract substring
string:n:m
extracts the m characters starting from the nth character in the string string
Note: the index of the first character is 0
string="helloworld"
str2=${string:2:4}
echo ${str2}
# llow
find substring
Find the position of the character i or o (whichever letter comes first is counted)
string="runoob is a great site"
echo `expr index "$string" io`
# 4