为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用双或单括号、大括号

如何使用双或单括号、大括号

慕的地10843 2019-06-12 20:16:40
如何使用双或单括号、大括号我对括号、大括号在Bash中的用法以及它们的双形式或单一形式之间的区别感到困惑。有明确的解释吗?
查看完整描述

3 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

  1. 单一括号([)通常实际上调用一个名为[man testman [想了解更多信息。例子:

    $ VARIABLE=abcdef
    $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fiyes
  2. 双括号([[)做相同的事情(基本上)与一个单一的括号,但是一个bash内建。

    $ VARIABLE=abcdef
    $ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fino
  3. 括号(())用于创建子shell。例如:

    $ pwd/home/user 
    $ (cd /tmp; pwd)/tmp
    $ pwd/home/user

    如您所见,子shell允许您在不影响当前shell环境的情况下执行操作。

  4. (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

有一个微妙的句法区别( )不过,(见Bash参考);本质上,分号;在大括号中的最后一个命令之后是必须的,而大括号{} 被空间包围。


查看完整回答
反对 回复 2019-06-12
?
撒科打诨

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] http:/wiki.bash-hackers.org/Scripting/过时

卷曲架

${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


查看完整回答
反对 回复 2019-06-12
  • 3 回答
  • 0 关注
  • 3088 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信