Shell- variable operation

1. String operations

1.1 Syntax

image

1.2 Example

1) acquisition variable length

VAR="hello world"
echo $ {# VAR}

[root@o18c tmp]# VAR="hello world"
[root@o18c tmp]# echo ${#VAR}
11

2) sub-sequence by an index subscript taken under

echo $ {VAR: 6}
echo $ {VAR: 6: 3}

[root@o18c tmp]# echo ${VAR:6}
world
[root@o18c tmp]# echo ${VAR:6:3}
wor
[root@o18c tmp]#

3) Find a specified character interception substring

VAR='https://www.mirrors163.com'
# Start from the left found the characters '//' and all the characters to the left and remove itself, namely its right to retain all of the characters
echo $ {VAR # * //}
echo $ {VAR # * \.}

# Begin to find the right one from the left most characters '//' and all the characters to the left and remove itself, namely the right of all the characters retain their character
echo $ {VAR ## * \.}

# Begun to find characters 'www' from the right and the right of all the characters and their own deleted, all the characters retain their left
echo $ {VAR% www *}
echo $ {VAR% \. *}
# Begin to find the right one from the leftmost character '' and the right of all the characters and their own deleted, all the characters retain their character left
echo $ {VAR %% \. *}

# Is to remove the character to the left (on the left side of the keyboard # $ it)
% Is to remove the right side (the right side of the keyboard of $%)
Single symbol is a minimum matching; two symbols is the maximum matching.
* Is used to match the characters do not, that is, want to get rid of that part of the
There are specified character delimiter, and * cooperation, which decided to take part
Note: The string to find support regular mode

[root@o18c tmp]# VAR='https://www.mirrors163.com'
[root@o18c tmp]# echo ${VAR#*//}
www.mirrors163.com
[root@o18c tmp]# echo ${VAR##*\.} 
with
[root@o18c tmp]# echo ${VAR#*\.}
mirrors163.com
[root@o18c tmp]# echo ${VAR%www*}
https://
[root@o18c tmp]# echo ${VAR%%\.*}
https://www
[root@o18c tmp]# echo ${VAR%\.*}
https://www.mirrors163
[root@o18c tmp]# echo ${VAR%%\.*}
https://www
[root@o18c tmp]#

4) variable substitution

Use # 'baidu' replacement characters of 'mirrors163'
echo ${VAR/mirrors163/baidu}
echo $ {VAR / m / M}
Use # 'M' replace all characters in the character 'm'
echo $ {VAR // m / M}

[root@o18c tmp]# echo ${VAR/mirrors163/baidu}
https://www.baidu.com
[root@o18c tmp]# echo ${VAR/m/M}
https://www.Mirrors163.com
[root@o18c tmp]# echo ${VAR//m/M}
https://www.Mirrors163.coM
[root@o18c tmp]#

5) match the beginning or end of the character substitution

str="apple, tree, apple tree"
echo ${str/#apple/APPLE}
echo ${str/%tree/TREE}

# Find the beginning of "apple" substring of string str and replaced 'APPLE'
[root@o18c tmp]# echo ${str/#apple/APPLE}
APPLE, tree, apple tree
[root@o18c tmp]# echo ${str/%apple/APPLE}
apple, tree, apple tree
# Find the end of "tree" substring of string str and replaced 'TREE'
[root@o18c tmp]# echo ${str/%tree/TREE}
apple, tree, apple TREE
[root@o18c tmp]#

2. According to the state variable assignment

2.1 Syntax


2.2 Example

Guess you like

Origin www.cnblogs.com/binliubiao/p/12548160.html