What is the benefit of using $() instead of backticks in shell scripts?

后端 未结 8 1305
梦谈多话
梦谈多话 2020-11-22 05:05

There are two ways to capture the output of command line in bash:

  1. Legacy Bourne shell backticks ``:

    var=`command`
             
    
    
            
8条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 05:56

    From man bash:

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

提交回复
热议问题