${#parameter}:返回$parameter的长度;
0 1 2 3 4 |
[root@LAMP tmp]# BYRD="I am Byrd" [root@LAMP tmp]# echo $BYRD I am Byrd [root@LAMP tmp]# echo ${#BYRD} 9 |
${parameter:offset}:在$parameter中,从位置offset之后开始提取子串;
0 1 2 |
[root@LAMP tmp]# BYRD="I am Byrd" [root@LAMP tmp]# echo ${BYRD:2} am Byrd |
${parameter:offset:length}:在$parameter中从位置offset之后开始提取长度为length子串;
0 1 2 |
[root@LAMP tmp]# BYRD="I am Byrd" [root@LAMP tmp]# echo ${BYRD:5:4} Byrd |
${parameter#word}:从变量$parameter开头开始删除最短匹配word子串;
0 1 2 |
[root@LAMP tmp]# BYRD="I am Byrd I am ok I am" [root@LAMP tmp]# echo ${BYRD#I am} Byrd I am ok I am |
${parameter##word}:从变量$parameter开头开始删除最长匹配word子串;
0 1 2 |
[root@LAMP tmp]# BYRD="I am Byrd I am ok I am" [root@LAMP tmp]# echo ${BYRD##I am} Byrd I am ok I am |
${parameter%word}:从变量$parameter结尾开始删除最短匹配word子串;
0 1 2 |
[root@LAMP tmp]# BYRD="I am Byrd I am ok I am ok" [root@LAMP tmp]# echo ${BYRD%ok} I am Byrd I am ok I am |
${parameter%%word}:从变量$parameter结尾开始删除最长匹配word子串;
0 1 2 3 4 |
[root@LAMP tmp]# BYRD="I am Byrd I am ok I am ok" [root@LAMP tmp]# echo ${BYRD%%I am} I am Byrd I am ok I am ok [root@LAMP tmp]# echo ${BYRD%%I am ok} I am Byrd I am ok |
${parameter/pattern/string}:使用string,来代替第一个匹配的word;
0 1 2 |
[root@LAMP tmp]# BYRD="Byrd I am Byrd Byrd" [root@LAMP tmp]# echo ${BYRD/Byrd/hello} hello I am Byrd Byrd |
${parameter/#pattern/string}:如果$parameter前缀匹配word,就用string来代替匹配word;
0 1 2 |
[root@LAMP tmp]# BYRD="Byrd I am Byrd Byrd" [root@LAMP tmp]# echo ${BYRD/#Byrd/hello} hello I am Byrd Byrd |
${parameter/%pattern/string}:如果$parameter后缀匹配word,就用string来代替匹配word;
0 1 2 |
[root@LAMP tmp]# BYRD="Byrd I am Byrd Byrd" [root@LAMP tmp]# echo ${BYRD/%Byrd/hello} Byrd I am Byrd hello |
案例:取消_finished
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
[root@LAMP test]# touch 20140721161{1..10}_finished.jpg [root@LAMP test]# ll total 0 -rw-r--r--. 1 root root 0 Jul 23 15:03 2014072116110_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211611_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211612_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211613_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211614_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211615_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211616_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211617_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211618_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211619_finished.jpg [root@LAMP test]# echo ${pic%_finished*} 201407211611 [root@LAMP test]# echo ${pic%_finished*}.jpg 201407211611.jpg [root@LAMP test]# mv $pic ${pic%_finished*}.jpg [root@LAMP test]# ll total 0 -rw-r--r--. 1 root root 0 Jul 23 15:03 2014072116110_finished.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211611.jpg [root@LAMP test]# for pic in `ls *.jpg`;do mv $pic ${pic%_finished*}.jpg ;done [root@LAMP test]# ll total 0 -rw-r--r--. 1 root root 0 Jul 23 15:03 2014072116110.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211611.jpg.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211612.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211613.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211614.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211615.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211616.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211617.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211618.jpg -rw-r--r--. 1 root root 0 Jul 23 15:03 201407211619.jpg [root@LAMP test]# cat change_file.sh #!/bin/bash #example for pic in `ls *.jpg`; do mv $pic ${pic%_finished*}.jpg done [root@LAMP test]# touch {a..e}.html [root@LAMP test]# ll total 4 -rw-r--r--. 1 root root 0 Jul 23 15:22 a.html -rw-r--r--. 1 root root 0 Jul 23 15:22 b.html -rw-r--r--. 1 root root 0 Jul 23 15:22 c.html -rw-r--r--. 1 root root 0 Jul 23 15:22 d.html -rw-r--r--. 1 root root 0 Jul 23 15:22 e.html [root@LAMP test]# file="a.html" [root@LAMP test]# echo ${file/%html/HTML} a.HTML [root@LAMP test]# mv $file ${file/%html/HTML} [root@LAMP test]# ll -rw-r--r--. 1 root root 0 Jul 23 15:22 a.HTML [root@LAMP test]# for file in `ls *.html`; do mv $file ${file/%html/HTML};done [root@LAMP test]# for file in `ls *.HTML`;do mv $file `echo ${file%.html}|tr "[a-z]" "[A-Z]"`;done |
参考资料:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
${parameter:-word} Use Default Values. If parameter is unset or null, the expan- sion of word is substituted. Otherwise, the value of parameter is substituted. ${parameter:=word} Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of param- eter is then substituted. Positional parameters and special parameters may not be assigned to in this way. ${parameter:?word} Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. ${parameter:+word} Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted. ${parameter:offset} ${parameter:offset:length} Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter start- ing at the character specified by offset. length and offset are arithmetic expressions (see ARITHMETIC EVALUATION below). length must evaluate to a number greater than or equal to zero. If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter. If parameter is @, the result is length positional parameters beginning at offset. If parameter is an indexed array name sub- scripted by @ or *, the result is the length members of the array beginning with ${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. Substring expansion applied to an associative array produces undefined results. Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the :- expansion. Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1 by default. If offset is 0, and the positional parameters are used, $0 is prefixed to the list. ${!prefix*} ${!prefix@} Names matching prefix. Expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable. When @ is used and the expansion appears within double quotes, each variable name expands to a separate word. ${!name[@]} ${!name[*]} List of array keys. If name is an array variable, expands to the list of array indices (keys) assigned in name. If name is not an array, expands to 0 if name is set and null otherwise. When @ is used and the expansion appears within double quotes, each key expands to a separate word. ${#parameter} Parameter length. The length in characters of the value of parameter is substituted. If parameter is * or @, the value substituted is the number of positional parameters. If parame- ter is an array name subscripted by * or @, the value substi- tuted is the number of elements in the array. ${parameter#word} ${parameter##word} Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘‘#’’ case) or the longest matching pat- tern (the ‘‘##’’ case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parame- ter in turn, and the expansion is the resultant list. If param- eter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list. ${parameter%word} ${parameter%%word} Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘‘%’’ case) or the longest matching pattern (the ‘‘%%’’ case) deleted. If parameter is @ or *, the pattern removal operation is applied to each posi- tional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list. ${parameter/pattern/string} Pattern substitution. The pattern is expanded to produce a pat- tern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the begin- ning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / fol- lowing pattern may be omitted. If parameter is @ or *, the sub- stitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list. ${parameter^pattern} ${parameter^^pattern} ${parameter,pattern} ${parameter,,pattern} Case modification. This expansion modifies the case of alpha- betic characters in parameter. The pattern is expanded to pro- duce a pattern just as in pathname expansion. The ^ operator converts lowercase letters matching pattern to uppercase; the , operator converts matching uppercase letters to lowercase. The ^^ and ,, expansions convert each matched character in the expanded value; the ^ and , expansions match and convert only the first character in the expanded value.. If pattern is omit- ted, it is treated like a ?, which matches every character. If parameter is @ or *, the case modification operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the case modification operation is applied to each member of the array in turn, and the expansion is the resultant list. |
参考:http://wenku.baidu.com/view/f4182920482fb4daa58d4b40?fr=prin
申明:本文由BYRD原创(基于Centos6.4 X64),未经许可禁止转载!SourceByrd's Weblog-https://note.t4x.org/system/shell-variable-substring/ SourceByrd's Weblog-https://note.t4x.org/system/shell-variable-substring/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!