- 论坛徽章:
- 0
|
呵呵,看来会parmmeter substitution的人不是很多啊,建议好好看看
《Advanced Bash-Scripting Guide》
取前三个字符:
a="abcdefg"
echo ${a:0:3}
取后三个字符:
a="abcdefg"
echo ${a -3)}
中间三个字符:
a="abcdefg"
echo ${a:2:3}
Table B-4. Parameter Substitution and Expansion
Expression Meaning
${var} Value of var, same as $var
${var-DEFAULT} If var not set, evaluate expression as $DEFAULT *
${var EFAULT} If var not set or is empty, evaluate expression as $DEFAULT *
${var=DEFAULT} If var not set, evaluate expression as $DEFAULT *
${var:=DEFAULT} If var not set, evaluate expression as $DEFAULT *
${var+OTHER} If var set, evaluate expression as $OTHER, otherwise as null string
${var:+OTHER} If var set, evaluate expression as $OTHER, otherwise as null string
${var?ERR_MSG} If var not set, print $ERR_MSG *
${var ERR_MSG} If var not set, print $ERR_MSG *
${!varprefix*} Matches all previously declared variables beginning with varprefix
${!varprefix@} Matches all previously declared variables beginning with varprefix
* Of course if var is set, evaluate the expression as $var.
Table B-5. String Operations
Expression Meaning
${#string} Length of $string
${string:position} Extract substring from $string at $position
${string:position:length} Extract $length characters substring from $string at $position
${string#substring} Strip shortest match of $substring from front of $string
${string##substring} Strip longest match of $substring from front of $string
${string%substring} Strip shortest match of $substring from back of $string
${string%%substring} Strip longest match of $substring from back of $string
${string/substring/replacement} Replace first match of $substring with $replacement
${string//substring/replacement} Replace all matches of $substring with $replacement
${string/#substring/replacement} If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement} If $substring matches back end of $string, substitute $replacement for $substring
expr match "$string" '$substring' Length of matching $substring* at beginning of $string
expr "$string" : '$substring' Length of matching $substring* at beginning of $string
expr index "$string" $substring Numerical position in $string of first character in $substring that matches
expr substr $string $position $length Extract $length characters from $string starting at $position
expr match "$string" '\($substring\)' Extract $substring* at beginning of $string
expr "$string" : '\($substring\)' Extract $substring* at beginning of $string
expr match "$string" '.*\($substring\)' Extract $substring* at end of $string
expr "$string" : '.*\($substring\)' Extract $substring* at end of $string |
|