concept
Strings are the most commonly used and useful data types in shell programming (except numbers and strings, there are no other types that are easy to use). Strings can be in single quotes, double quotes, or without quotes.
Single quotes declare string
- Any character in single quotes will be output as it is, and variables in single quotes strings are invalid;
- Single quotes cannot appear in single quotes (not after using escape characters for single quotes), but they can appear in pairs and be used as string splicing.
Double quotes declare string
- There can be variables in double quotes
- Escape characters can appear in double quotes
Example:
#The difference between single quotes and double quotes using variables
root@master:~$ name='huangSir'
root@master:~$ echo '$name'
$name
root@master:~$ echo "$name"
huangSir
#Nested single quotes in single quotes
root@master:~$ echo ''hello world''
hello world
#Nested single quotes in double quotes
root@master:~$ echo "'hello world'"
'hello world'
#The difference between single quotes and double quotes using escaped characters
root@master:~$ echo '\"hello world\"'
\"hello world\"
root@master:~$ echo "\"hello world\""
"hello world"
Various operations of strings
In Shell scripts, strings are common data types, and strings can be processed in many ways, including splicing, intercepting, substitution, comparison and other operations.
String stitching
String stitching can be directly spliced without any special characters in the middle
root@master:~$ str1="hello"
root@master:~$ str2="world"
root@master:~$ str3="$str1 $str2"
root@master:~$ echo $str3
hello world
root@master:~$ str3="${str1} like ${str2}"
root@master:~$ echo $str3
hello like world
Get the string length${#str}
Statistics character length (how many characters are there in a variable)
#Spaces are also considered characters
root@master:~$ str="I Like Shell"
root@master:~$ echo ${#str}
12
Intercept string${str:index:index}
root@master:~$ str="hello world"
# Start with the index subscript of 5, intercept backwards, discard the index subscript of 5
root@master:~$ echo ${str:5}
world
# Start with the index subscript of 2 and intercept 5 characters backwards
root@master:~$ echo ${str:2:5}
llo w
# Start with the index subscript of 2 and intercept the 6th character to the last
root@master:~$ echo ${str:2:-6}
llo
Starting from the index subscript of 2, the 7th character to the end is intercepted
root@master:~$ echo ${str:2:-7}
ll
String replacement
${str/newStr/oldStr}
WillnewStr
Replace witholdStr
, only replace the first one encounteredoldStr
root@master:~$ str="hello world"
root@master:~$ echo ${str/l/o}
heolo world
${str//newStr/oldStr}
WillnewStr
Replace witholdStr
, replace alloldStr
root@master:~$ str="hello world"
root@master:~$ echo ${str//l/o}
heooo worod
String case conversion
Requires a version of bash4+
Caps conversion:${str^^}
root@master:~$ str="hello world"
root@master:~$ echo ${str^^}
HELLO WORLD
Lowercase conversion:${str,,}
root@master:~$ str="HELLO WORLD"
root@master:~$ echo ${str,,}
hello world
Split string into array
root@master:~$ str="apple:banana:orange"
#According to:Split
root@master:~$ IFS=':' read -ra arr <<< "$str"
root@master:~$ echo ${arr[0]}
apple
root@master:~$ echo ${arr[1]}
banana
Remove prefix or suffix
-
${str#word}
: Start deleting from the left side of the variable and delete according to the shortest match -
${str##word}
: Start deleting from the left side of the variable and delete according to the longest match -
${str%word}
: Start deleting from the right side of the variable and delete according to the shortest match -
${str%%word}
: Start deleting from the right side of the variable and delete according to the longest match
Example:
root@master:~$ str=""
root@master:~$ echo ${str#*.}
root@master:~$ echo ${str##*.}
gz
root@master:~$ echo ${str%.*}
root@master:~$ echo ${str%%.*}
file
Determine whether substrings are included
if [[ $str == *World* ]]; then
echo "Includes World"
fi
# or use regular expressions
if [[ $str =~ .*World.* ]]; then
echo "Match Successfully"
fi
Determine whether the string is empty
[[ -z "$str" ]]
: Return to true for empty[[ -n "$str" ]]
: Non-empty returns to true
root@master:~$ name=
root@master:~$ [[ -z "$name" ]] && echo true || echo false
true
root@master:~$ [[ -n "$name" ]] && echo true || echo false
false
Determine whether two strings are equal
[[ $str1 == $str2 ]]
: Equal judgment[[ $str1 != $str2 ]]
: Unable to judge
root@master:~$ [[ "hello" == "hello" ]] && echo true || echo false
true
root@master:~$ [[ "hello" != "hello" ]] && echo true || echo false
false