变量替换操作
-
${VAR:-value}
如果变量为空,返回新值。
-
${VAR:=value}
如果变量为空,返回新值且付给变量。
-
${VAR:?message}
如果变量为空,显式一条消息。
-
${VAR:offset:length}
截取变量的一部分。
模式匹配操作
-
${VAR#pattern}
匹配第一个,返回后面的。
-
${VAR##pattern}
匹配最后一个,返回后面的。
-
${VAR%pattern}
从后向前匹配第一个,返回前面的。
-
${VAR%%pattern}
从后向前匹配至最前,返回前面的。
#!/bin/bash
PASSWD_PATH=$(which passwd)
echo "\$PASSWD_PATH is $PASSWD_PATH"
echo "\${PASSWD_PATH#*/} is ${PASSWD_PATH#*/}"
echo "\${PASSWD_PATH##*/} is ${PASSWD_PATH##*/}"
echo "\${PASSWD_PATH%/*} is ${PASSWD_PATH%/*}"
echo "\${PASSWD_PATH%%/*} is ${PASSWD_PATH%%/*}"
正则表达式
-
^text
- Line starts with text
-
text$
- Line ends with text
-
.
- Wildcard, match any single character
-
[abc], [a-c]
- Matchs a, b or c.
-
*
- Matches 0 or an infinitive number of the previous character.
-
\{2\}
- match exactly 2 of the previous character.
-
\{1, 3\}
- minimum 1 and maximum 3 of the previous character.
-
colou?r
- color or colour
数值计算
- $((1 + 1))
- let X="$1 $2 $3"; echo $X
- echo "scale=9; 10/3" | bc