如何使用双或单括号、大括号我对括号、大括号在Bash中的用法以及它们的双形式或单一形式之间的区别感到困惑。有明确的解释吗?
3 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
单一括号( [
)通常实际上调用一个名为 [
;man test
或 man [
想了解更多信息。例子: $ VARIABLE=abcdef $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fiyes
双括号( [[
)做相同的事情(基本上)与一个单一的括号,但是一个bash内建。 $ VARIABLE=abcdef $ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fino
括号( ()
)用于创建子shell。例如: $ pwd/home/user $ (cd /tmp; pwd)/tmp $ pwd/home/user
如您所见,子shell允许您在不影响当前shell环境的情况下执行操作。 (A)Braces( {}
)用于明确地识别变量。例子: $ VARIABLE=abcdef $ echo Variable: $VARIABLEVariable: abcdef $ echo Variable: $VARIABLE123456Variable:$ echo Variable: ${VARIABLE}123456Variable: abcdef123456
(B)BRACE还用于执行 电流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
( )
;
{
, }
必
撒科打诨
TA贡献1934条经验 获得超2个赞
托架
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]
卷曲架
${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
- 3 回答
- 0 关注
- 3244 浏览
添加回答
举报
0/150
提交
取消