Which command substitution do you prefer in shell scripts: backquotes `` or $() syntax?

a year ago by kiri to c/linux

Looks like $(...) is better, but I still pretty often see `...` in some articles.

IanTwenty 18 points a year ago

$() for me, to quote from

https://www.shellcheck.net/wiki/SC2006

Backtick command substitution `...` is legacy syntax with several issues.

  1. It has a series of undefined behaviors related to quoting in POSIX.
  2. It imposes a custom escaping mode with surprising results.
  3. It's exceptionally hard to nest.

$(...) command substitution has none of these problems, and is therefore strongly encouraged.

path: 0 20471878, hotness: undefined, score: 18, children: 3
confusedpuppy 4 points a year ago

Shellcheck is a great tool for scripting.

When I'm building a new script, I usually add the following function to the script and run the function before anything else. The script will exit immediately if any issues are found so I have a chance to correct things. If no issues are found, the script will simply continue.

It's small and simple so it's easy to remove when I'm done building a script.

script_check() {
    if ! shellcheck "${0}"; then
        exit 1
    fi
}

script_check

Shellcheck has helped me learn a lot about scripting and I strongly recommend using it too.

path: 0 20471878 20472726, hotness: undefined, score: 4, children: 2
IanTwenty 5 points a year ago

That's good. There are also editors that can run it for you and highlight the issues whilst you type, neovim being one.

path: 0 20471878 20472726 20472796, hotness: undefined, score: 5, children: 0
nous 3 points a year ago

Why wrap it in a function at all? Why not just put the if at the top of the file?

path: 0 20471878 20472726 20473362, hotness: undefined, score: 3, children: 0
HelloRoot 18 points a year ago

never backquotes, it's unintuitive

path: 0 20467883, hotness: undefined, score: 18, children: 0
30p87 10 points a year ago

$()

Backticks are pretty old syntax.

path: 0 20467897, hotness: undefined, score: 10, children: 1
MonkderVierte 1 point a year ago

Deprecated btw.

path: 0 20467897 20470150, hotness: undefined, score: 1, children: 0
red_bull_of_juarez 7 points a year ago

I'm using fish, so it's ().

path: 0 20468642, hotness: undefined, score: 7, children: 1
trevor 2 points a year ago

I prefer this as well, but just adding that Fish also supports $() for those that don't know.

I keep hearing people that haven't used modern versions of Fish say that it's somewhat different from Bash and strays from POSIX compatibility quite a bit. While the latter is true, Fish has added many bash-isms over the years, so most scripting idioms you're familiar with will work there too.

path: 0 20468642 20470279, hotness: undefined, score: 2, children: 0
excess0680 3 points a year ago

By some sheer coincidence, I searched this topic today. I’ve been a consistent user of the parenthesis expansion, but never thought of why I preferred one or the other.

I suppose the primary advantage is that $() will expand in a consistent way. You can even nest quotes and more expansions in one, while you’d struggle the same with backtick notation.

So I’ll just keep using parentheses.

path: 0 20469087, hotness: undefined, score: 3, children: 0
Moss_the_TeXie 1 point a year ago

@kiri
$()
Better reachable on german keyboard. Also muscle memory … YmmMV. ;-)

path: 0 20473014, hotness: undefined, score: 1, children: 0
aubeynarf -1 points a year ago
path: 0 20468102, hotness: undefined, score: -1, children: 0
linux
linux

@programming.dev

login for more options
15211
5335
3306

A community for everything relating to the GNU/Linux operating system (except the memes!)

Also, check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

go to feed...