You are not logged in.
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
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
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
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)...
Offline
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 . 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
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
. 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
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