0. string of small knowledge
String may be composed of single quotes ' '
enclosed in double quotes may be " "
enclosed in quotation marks may not, there is a difference between them:
1) a single quotes ' '
surrounded string:
- Any characters will be output as , in which a variable is invalid.
- String can not appear single quotation marks , even on single quote escape is not OK .
2) a double quotes " "
surround string:
- If contains a variable, then that variable will be resolved (to get the value of the variable), rather than as it is output.
- String can appear in double quotes , as long as it escaped on the line .
3) is not a quoted string
- Appears in the string is not surrounded by quotes in the variable time to time will be resolved , and this double-quoted
" "
strings enclosed by the same. - String can be no spaces , otherwise the space behind the other strings as variable or command parsing.
Get string length may be $ {# string_name}
1. String taken
1.1 taken from the specified start location
principle | The original string |
result |
---|---|---|
The first character to the left is indicated by 0, the right of the first character is represented by 0-1, the starting number is 1 ${string: start :length} start: length specified starting position and length, the length of the omitted word represents until the end of the string |
$ echo out the { the var : 0 to : 5 } |
http: |
$ echo out the { the var : 7 } |
www.aaa.com/123.html | |
$ echo out the { the var : 0 to - 7 : 3 } |
23. | |
$ echo out the { the var : 0 to - 7 } |
23.html |
1.2 taken from the specified character (substring) Start
principle | The original string var=http://www.aaa.com/123.html |
result |
---|---|---|
# ## represents the start deleting from the left. A # means to delete from the left to the first specified character; Two # means to delete from the left to the last character specified. |
the var $ {# echo out the * / } | /www.aaa.com/123.html |
the var $ ## {echo out the * / } | 123.html | |
% %% indicates the start deleting from the right. A % said the right to delete from the first specified character; Liangge % said deleted from the left to the last character specified. Deleted includes the specified character itself. |
the var% $ {echo out the / * } | http://www.aaa.com |
the var %% $ {echo out the / * } | http: | |
If you do not ignore a character specifies the character left / right, then you can not write * Means zero or more of any character,? Represents only one match any character, [...] which matches the characters in brackets, [! ..] in brackets represent the characters do not match. |
echo ${var#http:/} the var% $ {echo out the 123html } |
/www.aaa.com/123.html http://www.aaa.com |
1.3 Summary of string interception
format | Explanation | For Li |
---|---|---|
${string: 0-start :length} | Starting on the right string string of start character, the interception of length characters to the right. | var=/home/wwwroot/ echo $var #result is /home/wwwroot/ echo ${var:5} #result is /wwwroot/ echo ${var: -5} #result is root/ echo ${var:(-5)} #result is root/ echo ${var:0-5} #result is root/ echo ${var:1-5} #result is oot/ |
${string: 0-start} | From the right string string of characters start capturing start until the end. Extracting from the right string starts, but must be used after the colon spaces or a number or entire num parentheses , such as $ {var: -2}, $ {var: 0-2} or $ {var: (- 2 )}. |
|
${string: start :length} | From the left string string of start character, the interception of length characters to the right. | var="http://www.runoob.com/linux/linux-shell-variable.html" s1 = $ {var %% t * } Results: h s2 = $ {var% t * } Results: http://www.runoob.com/linux/linux-shell-variable.h s3 = $ {var %% *. } Results: HTTP: // WWW s4=${var#*/} 结果:/www.runoob.com/linux/linux-shell-variable.html s5=${var##*/} 结果:linux-shell-variable.html |
${string: start} | 从 string 字符串的左边第 start 个字符开始截取,直到最后。 | |
${string#*chars} | 从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。 | |
${string##*chars} | 从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。 | |
${string%*chars} | 从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 左边的所有字符。 | |
${string%%*chars} | 从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 左边的所有字符。 |
1.4 按指定要求截取
ls -al | cut -d "." -f2 获取后缀名
echo $test|cut -c1-8
2.字符串的拼接
脚本 | 结果 | 注意点 | |
---|---|---|---|
拼接字符 | $value1=home $value2=${value1}"=" echo $value2 | home= | 1.在 Shell 中不需要使用任何连接符,将两个字符串并排放在一起就能实现拼接 2.变量名要加{},避免变量名与其他字符向混淆 |
拼接字符串 | var0=test var1=43 echo ${var0}${var1} |
test43 |
3.字符串的替换
语法:${变量/查找/替换值} 一个'/'表示替换第一个,'//'表示替换所有,当查找中出现了特殊字符"/",需要转移成"\/"。
test='c:/windows/boot.ini' | 脚本 | 结果 |
---|---|---|
替换第一个/ | echo ${test/\//\\} |
c:\windows/boot.ini |
替换所有/ | echo ${test//\//\\} |
c:\windows\boot.ini |