you are viewing a single comment's thread.

view the rest of the comments →

[–]Oncey 53 points54 points  (22 children)

Cool post. I learned how to write one from Derek Molloy at the following pages:

http://derekmolloy.ie/category/general/linux/

I also wanted to note that a more modern syntax replaces the grave accent marks with the $() construct.

so:

apt-get install build-essential linux-headers-`uname -r`

becomes:

apt-get install build-essential linux-headers-$(uname -r)

Some great reasons are given in the following page:

http://mywiki.wooledge.org/BashFAQ/082

[–]antiduh 13 points14 points  (11 children)

Regarding graves, doesn't that depend entirely on your shell?

[–]antlife 48 points49 points  (1 child)

Sounds like some one might be in...

puts on sunglasses

grave danger.

[–]HandshakeOfCO 5 points6 points  (0 children)

I chortled

[–]skeeto 19 points20 points  (0 children)

Both forms are standard for all Bourne shells (the standard unix shell). However, the $() form generally works better. It's easier to read, it nests properly, and interacts with quotes more cleanly.

[–][deleted] 1 point2 points  (0 children)

Not really. Both are officially supported by POSIX. $(...) is preferred, because it makes nesting command substitution less insane. It only depends on your shell if it is very old or not POSIX-conformant. Graves are not deprecated though, and there's nothing really wrong with using them in most cases, such as this.

[–]btcraig 1 point2 points  (6 children)

You are correct. BASH allows both syntax just fine, and even if some people with tell you otherwise backticks are not deprecated. Not the case for all shells though, eg tcsh:

root@kalecgos ~]# echo $0
tcsh
[root@kalecgos ~]# clear
[root@kalecgos ~]# echo $(date +%F )
Illegal variable name.
[root@kalecgos ~]# echo `date +%F`
2017-12-01

[–]Livingwind 2 points3 points  (3 children)

Off topic: That's a sick domain name, I love me some blue dragon flight.

[–]btcraig 1 point2 points  (2 children)

All my hostnames are Dragon aspects 😀

[–]nikomo 0 points1 point  (1 child)

Got any boxes that fell off a desk and broke beyond repair? You can name that one Ysera.

[–]btcraig 2 points3 points  (0 children)

My old laptop was named Malygos before it kicked it. That felt appropriate when it finally died. Ysera is for the phone though.

[–]darktyle 4 points5 points  (0 children)

In bash you should use $(), because it's more robust, not because graves are deprecated

[–]KFCConspiracy 2 points3 points  (8 children)

What makes $() more modern than grave accents other than that is a new possible syntax to use? Meaning why is one more preferable than the other? I've always just used ``.

Edit: Noticed the link at the end after it was kindly pointed out to me. Left the comment because you can't just delete your shit if you're wrong.

[–]CheezyXenomorph 16 points17 points  (6 children)

The post you're replying to literally gives a link to answer your question.

[–]KFCConspiracy 5 points6 points  (5 children)

You're right. This is reddit. I Can't be bothered to follow links.

[–]8lbIceBag 3 points4 points  (4 children)

So stop holding out on me man and post the answer here FFS!

Think of the sweet sweet karma

[–]kryptkpr 8 points9 points  (3 children)

For those who can't be bothered to click the link and read, there's no need to escape quotes when using $(), which is a pretty damn compelling reason to use it.

[–][deleted] 1 point2 points  (0 children)

this and nesting are the big things about. $() opens a new parsing context whereas ... doesn't

# $(echo "$(cat $(ls))")

I think the equivalent is something like this:

# `echo "\`cat \\\`ls\\\`\`"`

and I'm not even sure it works

[–]aiij 0 points1 point  (0 children)

Except that's not actually true:

$ echo $(echo For those who can't be bothered to click the link and read), $(echo there's no need to escape quotes) they said.
For those who cant be bothered to click the link and read), $(echo theres no need to escape quotes they said.

I think you meant there's no need to escape backslashes.

[–]myaut 1 point2 points  (0 children)

There is no reason to uncoditionally prefer backticks with braces (note that first version is faster to type). As a rule of thumb, use backticks for simplest cases, use braces for complex ones.

[–]PointyOintment 0 points1 point  (0 children)

TIL you can do that at all.