String operation
String slices
The first character of the string, numbered 0, the right character number is increased by 1 at a time
There are two syntaxes for taking string slices
${variable:position starting point}
Start at the specified position, intercept the substring until the end of the string
str="123456" substr=${str:4} # Print result is 56echo $substr
${variable:position starting point: length}
str="123456" substr=${str:2:5} # Print result is 3456echo $substr
String length
use${#Variable Name}
Can calculate the string length of the variable value
name="test" # 4 echo ${#name}
String replacement
Replace only the first matching string
use${variable/string/replaced with string}
To replace the first matching string
str="test..1.." replace=${str/../-} # test-1.. echo $replace
Replace all strings that meet the criteria
use${variable//string/replaced with string}
To replace all strings that meet the criteria
str="test..1.." replace=${str//../-} # test-1- echo $replace
The above is the detailed content of shell string operation (slice length replacement). For more information about shell string operation, please pay attention to my other related articles!