You are not logged in.

#1 2010-05-14 11:37:41

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

[solved] bash: difference `` and $()?

Hey,

i'm new to bash scripting but I'm using both of the syntaxes `` and $() quite often and they seem to do the same thing.
I believe `` just evaluates and with $() i can do an array of evaluating sequences but i'm not sure and i didn't want to dig in too deep into some bash guides right now.

Can you tell me where the difference is?

Last edited by demian (2010-05-14 13:16:52)


no place like /home
github

Offline

#2 2010-05-14 11:44:30

Ogion
Member
From: Germany
Registered: 2007-12-11
Posts: 367

Re: [solved] bash: difference `` and $()?

AFAIK you can nest $(one$(two$(three))) but now ``.

Ogion


(my-dotfiles)
"People willing to trade their freedom for temporary security deserve neither and will lose both." - Benjamin Franklin
"Enlightenment is man's leaving his self-caused immaturity." - Immanuel Kant

Offline

#3 2010-05-14 11:47:09

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,487
Website

Re: [solved] bash: difference `` and $()?

Ogion wrote:

AFAIK you can nest $(one$(two$(three))) but now ``.

That is the main difference.  For that reason the `` syntax is sort of depricated, but it has been in use so long that it will always be around.

Offline

#4 2010-05-14 13:14:00

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [solved] bash: difference `` and $()?

I don't think $() is portable outside of bash.  That's what I've heard, anyway.

Offline

#5 2010-05-14 16:37:08

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [solved] bash: difference `` and $()?

dash supports $(), and that's about as close to a strict POSIX shell as I've ever used.

Offline

#6 2010-05-14 16:40:36

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: [solved] bash: difference `` and $()?

zsh also supports $(). You should always use $(), it's nestable and easier to read.

Offline

#7 2010-05-14 17:54:18

briest
Member
From: Katowice, PL
Registered: 2006-05-04
Posts: 468

Re: [solved] bash: difference `` and $()?

man bash wrote:

To nest when using the backquoted form, escape the inner backquotes with backslashes.

But $() is indeed more readable.

Offline

#8 2010-05-14 19:36:43

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [solved] bash: difference `` and $()?

A quick web search turns up various relevant reasons, most already listed here.

note - Google doesn't seem to recognize '`' and '$()' in a query, so use 'backtick' instead.

Last edited by Peasantoid (2010-05-14 19:37:56)

Offline

#9 2010-05-14 19:57:22

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [solved] bash: difference `` and $()?

briest wrote:
man bash wrote:

To nest when using the backquoted form, escape the inner backquotes with backslashes.

But $() is indeed more readable.

Proof:

echo $(echo $(echo $(ls)))

vs.

echo `echo \`echo \\\`ls\\\`\``

Offline

#10 2010-05-14 20:23:46

eirika
Member
From: New York
Registered: 2009-09-14
Posts: 65

Re: [solved] bash: difference `` and $()?

Trent wrote:

I don't think $() is portable outside of bash.  That's what I've heard, anyway.

$() is adopted and recommended by POSIX, so protability should not be a problem.
Search for command substitution in google and see by yourself wink

Last edited by eirika (2010-05-14 20:24:57)

Offline

#11 2010-05-14 23:06:29

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [solved] bash: difference `` and $()?

Okay, okay, I can take a hint smile  Apparently I was misinformed.  I wonder where I picked that up.

Offline

Board footer

Powered by FluxBB