The case against backticks

I hate backticks. They have no place in modern shell programming.
Here is a couple of reasons why this is the case.

Not nestable

backticks, by their nature, are not nestable. You can not have a
command expansion inside another with back ticks.

command `foo ` bar ` baz`

Should the shell expand foo and baz or bar
and then foo <output of bar> baz? As it happens
it will run the first one. Using the modern command expansion syntax you
can write:

command $(foo $( bar ) baz)
command $(foo ) bar $( baz)

Backticks are invisible

The backtick symbol is too small and too easily confused with a
single quote to be used for writing maintainable code. The alternative
is significantly larger and therefore more obvious.

ls "'`!!`'"
ls "'$(!!)'"

Here is a zoomed version of the line above in my terminal:

Please consider using the bracketed version of command expansion
rather than backticks and make the world a nicer place

10 thoughts on “The case against backticks

  1. Joe Buck
    on said:

    Unfortunately, the $(command) form isn’t portable; it doesn’t work with /bin/sh on Solaris (though of course you can run a decent shell like bash or ksh on Solaris).  It does work on HP-UX.

    That means that, at least for GNU stuff we have to avoid it (the goal is for the apps to build on pretty much anything).

  2. Sadly, even in this day and age, Solaris’ /bin/sh still isn’t POSIX, which undermines the value of having a standard.

  3. I don’t know about you, but I do an awful lot of shell programming, mostly because most of the time it’s the quickest and easiest way of automating something.

  4. Solaris has a couple of these oddities. Which is extremely odd because Solaris is one of the few Unices that is still developed and can be considered modern. Nonetheless it doesn’t support trivial things like this which were even supported by now discontinued unices. This a lot of Java-related issues are reason why I developed a strong antipathy against Sun.

  5. You can nest backticks if you escape them…
    command `foo `bar` blah`
    you can even nest more by escaping yet again…
    command `foo `bar \`zoing\“ blah`
    etc.
    Admittedly it’s not as pretty as the newer way.

  6. Jeremy
    on said:

    Meh whatever, they’re still extremely useful to sysadmins who could give two shits about writing out a proper script to do a 2 second task. Even proper scripts setting variables with backticks is downright essential and soooo easy.

    Your first argument is totally invalid unless you’re doing something like ssh user@lala “for i in `cat /home/user/omglist` ; do awk ‘($1>=200) {print $1}’; done”. It can be escaped nearly everywhere else. And your second argument is just anal, buy some glasses.

    Only programmer zealots hate backticks, which makes me wonder why you’re even using bash?

  7. On Solaris, /bin/sh is not a POSIX shell, Korn shell (/bin/ksh) is the POSIX shell.  If you want POSIX portability, put that at the head of your scripts.  On Solaris the rest of the POSIX complient binaries are in /usr/xpg4/bin.  You’ll note that the sh in that dir is a link to /bin/ksh:

    # ls -li /usr/xpg4/bin/sh /bin/ksh
      307 -r-xr-xr-x  3 root  bin  209288 Jan 25  2008 /bin/ksh
      13512 -r-xr-xr-x  1 root  bin  209288 Jan 25  2008 /usr/xpg4/bin/sh

    And if you can’t tell the difference between the various quote marks, you need to be using a better font.

  8. I for one detest backticks. I didn’t hate bash altogether, at least not at first, but I must also say that the syntax of the case/switch statement it’s fairly horrid. That said, I’m attempting to learn some C++ and am well fed up with writing compounded if/else statements. I’m also not a zealot, not even about my beloved bourbon.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.