this post was submitted on 13 Nov 2025
14 points (93.8% liked)

Linux

10091 readers
960 users here now

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

founded 2 years ago
MODERATORS
 

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

top 13 comments
sorted by: hot top controversial new old
[–] IanTwenty@piefed.social 11 points 7 hours ago* (last edited 7 hours ago) (1 children)

$() 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.

[–] confusedpuppy@lemmy.dbzer0.com 4 points 6 hours ago (2 children)

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.

[–] IanTwenty@piefed.social 5 points 6 hours ago

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

[–] nous@programming.dev 1 points 5 hours ago

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

[–] thingsiplay@beehaw.org 7 points 9 hours ago

Backticks are not very distinct, as they can be confused with single quotes (depending on the font). Also $() shows the beginning and end of the command very clearly. This is even more important when you have multiple of them in some variable in example. Not to mention that you can nest $(), but that is something I personally don't like to do anyway.

There are some functional differences too: https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html , such as that backtick command set as an alias in your bashrc will be executed once on load. While $() version is executed every time you execute the alias. Also backslash is literal in the backtick variants (with exception). All in all I never use backtick command substitution; it's confusing, limited and deprecated.

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

[–] HelloRoot@lemy.lol 17 points 13 hours ago

never backquotes, it's unintuitive

[–] 30p87@feddit.org 10 points 13 hours ago (1 children)

$()

Backticks are pretty old syntax.

[–] MonkderVierte@lemmy.zip 1 points 9 hours ago

Deprecated btw.

[–] red_bull_of_juarez@lemmy.dbzer0.com 5 points 11 hours ago (1 children)

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

[–] trevor@lemmy.blahaj.zone 1 points 8 hours 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.

[–] excess0680@lemmy.world 3 points 11 hours 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.

[–] aubeynarf@lemmynsfw.com -1 points 12 hours ago

backticks for sure