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

在shell脚本中使用$()而不是backticks有什么好处?

在shell脚本中使用$()而不是backticks有什么好处?

元芳怎么了 2019-07-05 14:41:25
在shell脚本中使用$()而不是backticks有什么好处?中有两种方法可以捕获命令行的输出。bash:遗留的Bourne壳背板``: var=`command`$()语法(据我所知,这是特定于Bash的,或者至少不受原始Bourne那样的非POSIX旧shell的支持) var=$(command)与Backticks相比,使用第二种语法有什么好处吗?还是两者完全等同?
查看完整描述

4 回答

?
繁华开满天机

TA贡献1816条经验 获得超4个赞

最重要的是它们是命令中的命令,而不是失去理智,试图找出某种形式的转义是否会在后台工作。

举个例子,虽然有些人为的:

deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print)

中的所有文件的列表。/dir目录树,与2011年12月开始的最早日期的文本文件同名(A).

另一个例子是获取父目录的名称(而不是完整路径):

pax> cd /home/pax/xyzzy/plugh
pax> parent=$(basename $(dirname $PWD))pax> echo $parent
xyzzy

(A)现在专一命令可能实际上不起作用,我还没有测试它的功能。所以,如果你对我投反对票,你就会忘记它的意图:-)这仅仅是为了说明你可以如何筑巢,而不是作为一个没有bug的现成的片段


查看完整回答
反对 回复 2019-07-05
?
开满天机

TA贡献1786条经验 获得超13个赞

假设您希望找到对应于何处的lib目录gcc已经安装好了。你可以选择:

libdir=$(dirname $(dirname $(which gcc)))/lib

libdir=`dirname \`dirname \\\`which gcc\\\`\``/lib

第一种比第二种更容易-使用第一种。


查看完整回答
反对 回复 2019-07-05
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

后排(`...`)是最古老的非POSIX兼容Bourne-shell和$(...)是否有POSIX和更多的优先考虑,原因如下:

  • 反斜杠(\)内背以一种不明显的方式处理:

    $ echo "`echo \\a`" "$(echo \\a)"a \a
    $ echo "`echo \\\\a`" "$(echo \\\\a)"\a \\a# Note that this is true for *single quotes* too!
    $ foo=`echo '\\'`; bar=$(echo '\\'); 
    echo "foo is $foo, bar is $bar" foo is \, bar is \\
  • 嵌套引用$()要方便得多:

    echo "x is $(sed ... <<<"$y")"

    而不是:

    echo "x is `sed ... <<<\"$y\"`"

    或者写这样的东西:

    IPs_inna_string=`awk "/\`cat /etc/myname\`/"'{print $1}' /etc/hosts`

    因为$()使用全新的上下文引用

    这是不可移植的,因为伯恩和科恩外壳将需要这些反斜杠,而巴什和破折号不需要。

  • 嵌套命令替换的语法更容易:

    x=$(grep "$(dirname "$path")" file)

    超过:

    x=`grep "\`dirname \"$path\"\`" file`

    因为$()强制执行一个全新的引用上下文,因此每个命令替换都受到保护,并且可以单独处理,而不需要特别关注引用和转义。当使用背面,它变得越来越丑后,两个以上的水平。

    很少有更多的例子:

    echo `echo `ls``      # INCORRECTecho `echo \`ls\``    # CORRECTecho $(echo $(ls))    # CORRECT
  • 它解决了使用反引号时行为不一致的问题:

    • echo '\$x'

      产出

      \$x

    • echo `echo '\$x'`

      产出

      $x

    • echo $(echo '\$x')

      产出

      \$x

  • Backticks语法对嵌入命令的内容有历史限制,不能处理一些包含反引号的有效脚本,而较新的$()表单可以处理任何类型的有效嵌入式脚本。

    例如,这些其他有效的嵌入式脚本不在左列中工作,而是在右侧工作。IEEE:

    echo `                         echo $(
    cat <<\eof                     cat <<\eof
    a here-doc with `              a here-doc with )eof                            eof`           
    
    
    echo `                         echo $(echo abc # a comment with `   
     echo abc # a comment with )`         
    
    
    echo `                         echo $(echo '`'                       echo ')'`

因此,$-前缀命令替换应该是首选的方法,因为它具有清晰的语法(提高了人和机器的可读性),它是不可见的和直观的,它的内部解析是分开的,而且它也更一致(与从双引号中解析的所有其他扩展),其中Backticks是唯一的例外和`字符相邻时很容易伪装。"使它更难读,特别是用小字体或不寻常的字体。

资料来源:为什么$(...)优先于`...`(后排)?在BashFAQ

另见:


查看完整回答
反对 回复 2019-07-05
?
桃花长相依

TA贡献1860条经验 获得超8个赞

        $(command)
   or          `command`

   Bash performs the expansion by executing command and replacing the com-
   mand  substitution  with  the  standard output of the command, with any
   trailing newlines deleted.  Embedded newlines are not deleted, but they
   may  be  removed during word splitting.  The command substitution $(cat
   file) can be replaced by the equivalent but faster $(< file).

   When the old-style backquote form of substitution  is  used,  backslash
   retains  its  literal  meaning except when followed by $, `, or \.  The
   first backquote not preceded by a backslash terminates the command sub-
   stitution.   When using the $(command) form, all characters between the
   parentheses make up the command; none are treated specially.


查看完整回答
反对 回复 2019-07-05
  • 4 回答
  • 0 关注
  • 908 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号