16

It is my understanding that the more modern $(...) command substitution syntax is preferred over the old `-based syntax, due to easier and less error-prone nesting and escaping syntax.

Further, it seems that most /bin/sh-style shells in modern use support $(…):

  • bash
  • ash (and therefore BusyBox, so most embedded Linux)
  • dash
  • FreeBSD /bin/sh

And $(…) is specified by IEEE 1003.1.

So I have 2 very related questions:

  • Is there any reason to use ` in new development of shell scripts unless you know of a specific old system that the script will need to run on?
  • Is there any reason not to teach UNIX programming students just to write $(...), and discuss ` only as an obsolete variant that they will likely encounter if they are reading other developers' shell scripts (and may need if they are working with a really old system or nonstandard for some reason)?
  • 7
    No, there's no point using or teaching `...`. It's there for backward portability with the Bourne shell only (like the Bourne shell had ^ (same as |) for backward portability with the Thomson shell). Note however that (t)csh don't have $(...) (but there's no much point using or teaching them either). – Stéphane Chazelas Jan 21 '15 at 21:26
  • 2
    It's a Groucho Marx thing `;{) – goldilocks Jan 21 '15 at 22:08
  • 2
    Wait, backticks are deprecated?? Does anybody have a canonical reference explaining why? (Weird syntax characters are very hard to search on.) – MathematicalOrchid Jan 22 '15 at 13:16
  • 1
    Easier to type. `` - 2 keys presses; $() - shift+4,9, shift+0 - 5 key presses; – Vi. Jan 22 '15 at 17:18
  • 3
    @MathematicalOrchid stackoverflow.com/questions/9405478/… seems pretty good. Backticks are very difficult to nest properly, especially when additional quotes are involved. – Michael Ekstrand Jan 26 '15 at 17:34
19

Since back-ticks are often used, it makes sense to teach this syntactic construct.

Of course, $() style command substitution should be emphasized as the default style (and standard conforming construct).

Why are back-ticks still popular? Because they save one character in typing, and they are arguably less heavy on the eye.

  • 18
    I wouldn't characterise "less heavy on the eye" as a feature, but a bug... – jasonwryan Jan 21 '15 at 21:35
  • 1
    Normally I use backsticks in simple cases, and $() in complex cases. But very lot scripts are using the backsticks until now, and the grads had to be able to scope with them. – peterh Jan 21 '15 at 22:14
  • @jasonwryan In that case the bug is not because of the brevity, but arbitrary issues. I.e., using backticks is easier on the eye, but that is not why it is depreciated. Unless you are trying to say sugar is by definition bad and we should all prefer asm for whatever... Actually I like the look of $() better but backticks are easier and more K.I.S.S.; could be pavlovian OCDish consequence of general programming. – goldilocks Jan 22 '15 at 3:13
  • @goldilocks once you get older, readability is so much harder anyway: backticks are practically invisible in code at my age... – jasonwryan Jan 22 '15 at 3:34
  • 3
    On a French keyboard for instance, ` is Altrg+è, (awkward to type), while $, (, ) don't need any modifier. – Stéphane Chazelas Jan 24 '15 at 18:03
5

I would not use them for programming, and teaching the use of backtick substitution in shell scripts as obsolete is fine (this seems to be the consensus). I don't think they're inherently evil, however, and (at least judging by your average tutorial to Linux command line) they are still frequently used in simple snippets/one-liners where they probably won't be nested and things you're only going to do once.

See Command substitution: backticks or dollar sign / paren enclosed?.

1

I avoid using the $() construct because it is not portable - in the real world. You have listed four shells - there are a lot more variations than that around (an order of magnitude?). Try running your script in Solaris /bin/sh and see how you go.

On the other hand, when do you stop supporting old systems? can you never progress to new ways of doing things? I reckon you should trust your own judgement, and if you can see a definite advantage of the new way over the old, then go for it ... this is not one of them (personally I find the bacticks much clearer - they cannot be confused with shell variable substitution, which is a different thing)

  • 1
    POSIX does not specify that /bin/sh must be conforming. The Solaris /bin/sh is indeed very non-conforming - although Solaris 9/10/... are officially POSIX compliant. The $() is just one construct it does not understand. But there are many more it doesn't understand or are interpreted differently. The portable way to get a conforming sh to search for it in the path returned by getconf PATH. – maxschlepzig Jan 24 '15 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.