$ echo f{oo,ee,a}d
food feed fad
$ mv error.log{,.OLD}
(error.log is renamed to error.log.OLD because the brace expression
expands to "mv error.log error.log.OLD")
$ for num in {000..2}; do echo "$num"; done
000
001
002
$ echo {00..8..2}
00 02 04 06 08
$ echo {D..T..4}
D H L P T
$ { date; top -b -n1 | head ; } >logfile
# 'date' and 'top' output are concatenated,
# could be useful sometimes to hunt for a top loader )
$ { date; make 2>&1; date; } | tee logfile
# now we can calculate the duration of a build from the logfile
if [ CONDITION ] Test construct
if [[ CONDITION ]] Extended test construct
Array[1]=element1 Array initialization
[a-z] Range of characters within a Regular Expression
$[ expression ] A non-standard & obsolete version of $(( expression )) [1]
${variable} Parameter substitution
${!variable} Indirect variable reference
{ command1; command2; . . . commandN; } Block of code
{string1,string2,string3,...} Brace expansion
{a..z} Extended brace expansion
{} Text replacement, after find and xargs
Parentheses
( command1; command2 ) Command group executed within a subshell
Array=(element1 element2 element3) Array initialization
result=$(COMMAND) Command substitution, new style
>(COMMAND) Process substitution
<(COMMAND) Process substitution
~:$ echo $SHELL
/bin/bash
~:$ echo ${#SHELL}
9
~:$ ARRAY=(one two three)
~:$ echo ${#ARRAY}
3
~:$ echo ${TEST:-test}
test
~:$ echo $TEST
~:$ export TEST=a_string
~:$ echo ${TEST:-test}
a_string
~:$ echo ${TEST2:-$TEST}
a_string
~:$ echo $TEST2
~:$ echo ${TEST2:=$TEST}
a_string
~:$ echo $TEST2
a_string
~:$ export STRING="thisisaverylongname"
~:$ echo ${STRING:4}
isaverylongname
~:$ echo ${STRING:6:5}
avery
~:$ echo ${ARRAY[*]}
one two one three one four
~:$ echo ${ARRAY[*]#one}
two three four
~:$ echo ${ARRAY[*]#t}
one wo one hree one four
~:$ echo ${ARRAY[*]#t*}
one wo one hree one four
~:$ echo ${ARRAY[*]##t*}
one one one four
~:$ echo $STRING
thisisaverylongname
~:$ echo ${STRING%name}
thisisaverylong
~:$ echo ${STRING/name/string}
thisisaverylongstring
6 回答
在Bash中,
test
和[
是内置的 .double bracket启用其他功能 . 例如,您可以使用
&&
和||
而不是-a
和-o
,并且有一个正则表达式匹配运算符=~
.除了分隔变量名之外,大括号还用于parameter expansion,因此您可以执行以下操作:
$ var="abcde"; echo ${var%d*}
abc
sed
的替换$ var="abcde"; echo ${var/de/12}
abc12
$ default="hello"; unset var; echo ${var:-$default}
hello
此外,大括号扩展创建字符串列表,这些字符串通常在循环中迭代:
请注意,在Bash 4之前,前导零和增量功能不可用 .
感谢gboffi提醒我关于支撑扩张的信息 .
双括号用于arithmetic operations:
它们使您可以省略整数和数组变量上的美元符号,并在运算符周围包含空格以提高可读性 .
单括号也用于array索引:
右侧(大多数/全部?)阵列参考需要使用卷曲支撑 .
ephemient's 评论提醒我,圆括号也用于子壳 . 并且它们用于创建数组 .
[
)通常实际上调用名为[
的程序;man test
或man [
了解更多信息 . 例:[[
)与单个括号完全相同(基本上),但是内置的是bash .()
)用于创建子壳 . 例如:如您所见,子shell允许您在不影响当前shell环境的情况下执行操作 .
4A . 大括号(
{}
)用于明确识别变量 . 例:4B . 大括号还用于在当前shell上下文中执行一系列命令,例如,
尽管如此(见bash reference),与
( )
存在微妙的句法差异;基本上,在大括号中的最后一个命令之后的分号;
是必须的,大括号{
,}
must 被空格包围 .Brackets
[1] http://wiki.bash-hackers.org/scripting/obsolete
Curly Braces
Parentheses
Double Parentheses
我只是想从TLDP添加这些:
test , [ 和 [[ 之间的区别在BashFAQ中有详细解释 .
结论是:
函数定义中的括号
括号
()
正在函数定义中使用:这就是你必须在命令参数中转义括号的原因: