如何使用双括号或单括号,括号,花括号
我对Bash中括号,括号,大括号的使用以及它们的双重或单一形式之间的差异感到困惑。 有明确的解释吗?
在Bash中, test
和[
内置。
双括号支持附加功能。 例如,您可以使用&&
和||
而不是-a
和-o
并且有一个匹配operator =~
的正则表达式。
大括号除了定义变量名称外,还用于参数扩展,因此您可以执行如下操作:
截断变量的内容
$ var="abcde"; echo ${var%d*}
abc
进行类似于sed
替换
$ var="abcde"; echo ${var/de/12}
abc12
使用默认值
$ default="hello"; unset var; echo ${var:-$default}
hello
还有更多
此外,大括号扩展会创建通常在循环中迭代的字符串列表:
$ 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
请注意,前导零和增量功能在Bash 4之前不可用。
感谢gboffi提醒我关于大括号扩展。
双括号用于算术运算:
((a++))
((meaning = 42))
for ((i=0; i<10; i++))
echo $((a + b + (14 * c)))
它们使您能够忽略整数和数组变量的美元符号,并在运算符周围包含空格以提高可读性。
单个括号也用于数组索引:
array[4]="hello"
element=${array[index]}
右边的(大多数/全部?)数组引用需要大括号。
ephemient的评论提醒我括号也用于subhells 。 并且它们被用来创建数组。
array=(1 2 3)
echo ${array[1]}
2
单个括号( [
)通常实际上调用名为[
; man test
或man [
更多信息。 例:
$ VARIABLE=abcdef
$ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi
yes
双括号( [[
)与单个括号完全相同(基本上),但是是bash内建的。
$ VARIABLE=abcdef
$ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fi
no
圆括号( ()
)用于创建子外壳。 例如:
$ pwd
/home/user
$ (cd /tmp; pwd)
/tmp
$ pwd
/home/user
如您所见,subshell允许您执行操作而不影响当前shell的环境。
4A。 大括号( {}
)用于明确标识变量。 例:
$ VARIABLE=abcdef
$ echo Variable: $VARIABLE
Variable: abcdef
$ echo Variable: $VARIABLE123456
Variable:
$ echo Variable: ${VARIABLE}123456
Variable: abcdef123456
4B。 大括号也用于在当前shell环境中执行一系列命令,例如
$ { 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
虽然( ( )
有一个细微的语法差异(参见bash参考); 基本上是一个分号;
在大括号内的最后一个命令是必须的,并且大括号{
, }
必须被空格包围。
括号
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]
[1] http://wiki.bash-hackers.org/scripting/obsolete
大括号
${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
括号
( 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
双括号
(( var = 78 )) Integer arithmetic
var=$(( 20 + 5 )) Integer arithmetic, with variable assignment
(( var++ )) C-style variable increment
(( var-- )) C-style variable decrement
(( var0 = var1<98?9:21 )) C-style ternary operation
链接地址: http://www.djcxy.com/p/17465.html
上一篇: How to use double or single brackets, parentheses, curly braces
下一篇: Assigning default values to shell variables with a single command in bash