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 (6) |

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

Name:


E-mail:


URL:


Comment:


Please enter "fudge" to prove you are a human