Wed, 17 May 2006

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

[] | # Read Comments (9) |

Comments

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).
Posted by Joe Buck at Wed May 17 23:50:51 2006
Sadly, even in this day and age, Solaris' /bin/sh still isn't POSIX, which undermines the value of having a standard.
Posted by JD at Thu May 18 08:03:31 2006
Isn't "modern shell programming" an oxymoron? B-)

/me ducks, runs
Posted by Evan Prodromou at Fri May 19 00:17:39 2006
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.
Posted by JD at Fri May 19 08:11:00 2006
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.
Posted by Chris at Fri Sep 8 02:58:01 2006
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.
Posted by Ian at Sun Jan 20 06:55:04 2008
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?
Posted by Jeremy at Fri Mar 6 09:34:25 2009
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.
Posted by Mike at Wed Feb 3 17:16:32 2010
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.
Posted by shiny666 at Thu Dec 23 01:09:52 2010

Name:


E-mail:


URL:


Comment:


Please enter "fudge" to prove you are a human