Results 1 to 4 of 4

Thread: Difference between backticks and $()

  1. #1
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Difference between backticks and $()

    I was writing a script some time ago and came across an occasion where
    Code:
    variable=`echo $hello | awk '{ gsub(/\\/, "/"); print }'`
    was not working... I desperately posted on the forums and it was suggested that I should use $():
    Code:
    variable=$(echo $hello | awk '{ gsub(/\\/, "/"); print }')
    Fortunately, that worked; unfortunately, the helpful individual never answered my question as to why this syntax worked and the other didn't. Thus I pose this question again to the great public: what is the difference between backticks and $() and in what contexts should each be used?
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

  2. #2
    Join Date
    Aug 2005
    Location
    Fargo, ND, USA
    Beans
    1,499
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Difference between backticks and $()

    I've never run into a situation before now where there was a difference between backticks and $(). I prefer $() because I think it's easier to read. OTOH, backticks are the standard. Only Bash supports $() AFAIK.

    The Bash manual doesn't say anything about backticks being treated differently than $(), except for maybe this vague statement:
    When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
    I guess that could mean that $() "quotes the command more", in the sense that backticks treat the command sort of like a string with the associated string processing.

    If I set hello to "C:\windows\fails\at\paths" and then run your code, I need to double escape the search pattern when using the backticks.

    Code:
    $ hello="C:\\windows\\fails\\at\\paths";  echo $hello
    C:\windows\fails\at\paths
    $ var=`echo $hello | awk '{ gsub(/\\/, "/"); print }'`; echo $var
    awk: line 1: runaway string constant "); print } ...
    bash: echo: write error: Broken pipe
    $ var=`echo $hello | awk '{ gsub(/\\\\/, "/"); print }'`; echo $var
    C:/windows/fails/at/paths
    $ var=$(echo $hello | awk '{ gsub(/\\/, "/"); print }'); echo $var
    C:/windows/fails/at/paths
    I have no idea if this is the problem you were seeing because you didn't supply enough information.
    Help yourself: Search the community docs or try other resources.
    Quote Originally Posted by Henry Spencer
    Those who do not understand Unix are condemned to reinvent it, poorly.
    Let science use your computer when you aren't: Folding@Home.

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Difference between backticks and $()

    if you have taken a look at the advance bash guide (under 3.4.5) (see my sig link for more), you can see there are some caveats with regard to backslashes. Furthermore, you are substituting backslash in awk. see here regarding uses of gsub with backslash. After reading and understand all that...you can be sure that it will work when you do this:
    Code:
    # hello="dkfafd\wklfafd\kdfasdfa"
    # goodbye=`echo "$hello" | awk '{ gsub(/\\\\/, "/"); print }'`
    # echo $goodbye
    dkfafd/wklfafd/kdfasdfa

  4. #4
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: Difference between backticks and $()

    Sorry if you thought that the provided background was insufficient, jpkotta. In any case, your comment was very helpful! Thanks for confirming and showing me the awesome documentation ghostdog74...
    Case closed...
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •