The content of this section:
Shell string Interception method
1, remove the leftmost character of the string
[[Email protected] ~]$ VI test.sh1Str="ABCD" 2str=${str#"a"} 3Echo $STR4str=${str%"D"} 5echo $STR Execute script [[email protected]~]$ ./test.sh BCD BC
2, the symbol # indicates the leftmost;% denotes the left and right side; Note the reference to the variable inside the curly brace, preceded by the unsigned $
String interception is not performed if the character string followed by # or% does not match the leftmost or most right value.
[Email protected] ~]$ vi test.sh 1 str="abcd" 2 str=${str#"b"} 3 Echo $STR 4 str=${str%"D"} 5 Echo $STR
Execute script
[Email protected] ~]$./test.sh ABCD ABC
3, experience the interception of multiple characters, the following script will intercept the leftmost two characters AB
[Email protected] ~]$ vi test.sh 1 str="abcd" 2 str=${str#"ab" } 3 echo $STR 4 str=${str%"C" } 5 Echo $STR
Execute script
[[email protected] ~]$./test.sh CD
Shell Intercept String Instance tutorial