== An appreciation for the Posix _$( )_ command substitution syntax The Posix standards for Unix sometimes get a bad rap (there are some crazy things in there, and some not so great ones), especially for the bits that they invented, but every so often I think that they did something absolutely right. The new Posix _$(...)_ syntax for command substitution is one shining example of this; it is superior to the original Bourne shell syntax of _`...`_ in so many ways. Here's some of the reasons that I like it. First off, _$(...)_ nests in a straightforward way without needing special quoting. All by itself this is a huge advantage; I've [[written before ../programming/BourneQuotingII]] that any time you have multiple levels of quoting and processing going on, things go to hell. Nested _$(...)_'s let you avoid all of that; you don't need to quote anything, you just nest them and then read them outside in (or inside out). You can easily extract an inner _$(...)_ to run it standalone to make sure that it returns the right result, and so on. Next, _$(...)_ is (in my opinion) easier to read and scan, because the edges of the command substitution are much easier to spot. _(_ and _)_ are much more visually distinctive than _`_, and run very little risk of being confused with other characters (unlike _`_ with _'_ and _"_). It's also clear whether you're dealing with the start or the end of a command substitution, unlike the Bourne shell case where a stray _`_ might be doing either and you need to carefully watch the surrounding context. Finally, it's easier to see and remember that _$(...)_ will expand inside _"..."_-quoted strings, because it looks just like other _$_ expansions. With traditional Bourne shell you had to remember that both _$_ and _`...`_ expanded in that context. (This cuts both ways when writing shell scripts; I find that I'm much more likely to use _$()_ inside double quotes than I ever was with _`...`_, simply because it's easier to remember that it's possible.) PS: before I started writing this entry, I had the incorrect impression that _$(...)_ originated in bash. Bash is probably still the most common Bourne shell variant to find it supported (and I see that Solaris _sh_ doesn't support it, [[not that this surprises me any more ../solaris/SolarisShBraindamage]]), but there are a number of others such as _dash_ and various versions of Kenneth Almquist's sh implementation (originally called _ash_).