You are not logged in.

#1 2013-03-11 18:46:04

the sad clown
Member
From: 192.168.0.X
Registered: 2011-03-20
Posts: 837

Confusion about VAR='command' and VAR=$(command)

I've been trying to learn scripting, and multiple sources have told me that 'command' and $(command) are equivalent, but it doesn't work that way for me:

me, ~ » date=$(date)
me, ~ » $date
bash: Mon: command not found
me, ~ » echo "The date is $date"
The date is Mon Mar 11 13:39:05 CDT 2013
me, ~ » date='date'
me, ~ » $date
Mon Mar 11 13:39:45 CDT 2013
me, ~ » echo "The date is $date"
The date is date

Can someone explain to me the difference between these two constructions?

Last edited by the sad clown (2013-03-11 18:48:49)


I laugh, yet the joke is on me

Offline

#2 2013-03-11 18:48:36

nagaseiori
Member
Registered: 2012-09-22
Posts: 72

Re: Confusion about VAR='command' and VAR=$(command)

AFAIK, $() runs the command in a subshell, while `` does not. The `` syntax has been deprecated, IIRC.

Edit: You should not be using ' ' to put the output of a command in a variable. ' ' simply stores, the literal string, which gets executed as a command when you access it.

Last edited by nagaseiori (2013-03-11 18:50:24)

Offline

#3 2013-03-11 18:53:47

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: Confusion about VAR='command' and VAR=$(command)

The ` ` syntax also has nesting issues.

$ cat foo
bar
$ cat bar
foo
$ cat $(echo $(cat foo))
foo
$ cat `echo `cat foo``
cat: cat: No such file or directory
bar

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#4 2013-03-11 19:48:04

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Confusion about VAR='command' and VAR=$(command)

See Greg's wiki on why $() is preferred over backticks.

If you are going to learn bash scripting, this site is the best reference on the web. Start with the Bash Guide and then work your way through the FAQs (from #bash)...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2013-03-11 22:55:38

deepsoul
Member
From: Earth
Registered: 2012-12-23
Posts: 67
Website

Re: Confusion about VAR='command' and VAR=$(command)

Uhhh... unless my eyes deceive me, the sad clown did not use any backticks, but used single quotes by mistake.  As pointed out in nagaseiori's post scriptum, this only assigns the word "date" to the variable "date".  The shell expands $date wherever it is on the command line.  In this case, the variable is in the position of the command, so its content, "date" is executed as a command.

If you use backticks (ASCII 0x60, not 0x27), you get the same thing as for $():

vs@schizo, ~ > date=`date`
vs@schizo, ~ > echo "The date is $date" 
The date is Mon Mar 11 23:44:39 CET 2013

As others have pointed out, backticks have their drawbacks, but I use them anyway big_smile.  On a US-layout keyboard, the backtick is on the top left, the same key as the tilde, but without shift.


Officer, I had to drive home - I was way too drunk to teleport!

Offline

#6 2013-03-30 03:46:55

the sad clown
Member
From: 192.168.0.X
Registered: 2011-03-20
Posts: 837

Re: Confusion about VAR='command' and VAR=$(command)

deepsoul wrote:

Uhhh... unless my eyes deceive me, the sad clown did not use any backticks, but used single quotes by mistake.  As pointed out in nagaseiori's post scriptum, this only assigns the word "date" to the variable "date".  The shell expands $date wherever it is on the command line.  In this case, the variable is in the position of the command, so its content, "date" is executed as a command.

If you use backticks (ASCII 0x60, not 0x27), you get the same thing as for $():

vs@schizo, ~ > date=`date`
vs@schizo, ~ > echo "The date is $date" 
The date is Mon Mar 11 23:44:39 CET 2013

As others have pointed out, backticks have their drawbacks, but I use them anyway big_smile.  On a US-layout keyboard, the backtick is on the top left, the same key as the tilde, but without shift.

Thanks for explaining that.  I was indeed mistaking single quotes ' ' for backticks ` `.


I laugh, yet the joke is on me

Offline

#7 2013-03-30 05:05:27

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: Confusion about VAR='command' and VAR=$(command)

the sad clown wrote:

me, ~ » date=$(date)
me, ~ » $date
bash: Mon: command not found

you tried to run the return from the date command as a command. That's why you got
"bash: Mon: command not found"

[~]$ today=$(date)

[~]$ $today
bash: Sat: command not found

[~]$ echo $today
Sat Mar 30 14:40:48 EST 2013

Then you set date to be the string "date" and made it a command by running it on the next line. An echo $date would return 'date' this time, not the actual date.

[~]$ today='date'

[~]$ $today
Sat Mar 30 14:50:43 EST 2013

[~]$ echo $today
date

[~]$ today='yesterday'

[~]$ $today
bash: yesterday: command not found

[~]$ echo $today
yesterday

HTH


You're just jealous because the voices only talk to me.

Offline

Board footer

Powered by FluxBB