Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 11-19-2009, 09:29 PM   #1
Registered: Oct 2007
Posts: 98

Rep: Reputation: 16
Question about variables in BASH scripts

A few random questions:

What is the difference between:

VAR=`date +%F_%H%M%S`


VAR=$(date +%F_%H%M%S)

Also, this question may be related:

If the output of a command is set as a variable in the beginning of the script, and later during that script the output of the original command that became the variable changes, does the variable change when it is called upon? In other words, when the command for the variable is ran to set the variable, is it simply "remembered" and static for the remainder of the script, or would that command be ran every time that variable is called in the script?

1 #!/bin/sh
2 CURDATE=`date +%m-%d-%y`
3 blah blah blah
4 somecommand 1> $CURDATE.stdout
Assume line two happens on Tuesday, but the script does not get to line 4 until the next day, Wednesday. Would that stdout file have the date of Tuesday even though it happened on Wednesday, or would the date command within the $CURDATE variable be re-ran when it is called upon and have the date of Wednesday?
Old 11-19-2009, 09:37 PM   #2
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS6
Posts: 267

Rep: Reputation: 58
There is no difference in how the ` ` or $() works. I prefer the second form as it is easier to see with my failing eyes. Once the variable is assigned, it stays the same until reassigned.
Old 11-19-2009, 09:39 PM   #3
Registered: Jun 2008
Posts: 31

Rep: Reputation: 16
var=`command` and var=$(command) are the same.

however i feel it's better practice to use $(), it keeps the script neater.

re. your 2nd question, i think the variable is set according to the output of that specific instance. if the output of the same command changes later on in the script, unless you re-set the variable, i don't think it will change.

could be wrong here....
Old 11-19-2009, 09:43 PM   #4
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551

The above link was given me by catkin earlier today; it explains a few reasons why $() is better than using `backticks`.

As to the 2nd question, the answer is: the variable will remain the same, and does not get reassigned, so your example would still contain "Tuesday" unless you re-ran line#2 on Wednesday or another day.

Old 11-19-2009, 09:53 PM   #5
Registered: Oct 2007
Posts: 98

Original Poster
Rep: Reputation: 16
Three answers that agree with each other. Awesome.

Of course this proves the concept of the first question:

echo `date +%r`
echo $(date +%r)

That link makes it very clear, and btw GrapefruiTgirl, the first question came from seeing another one of your posts a few minutes ago.
Old 11-19-2009, 10:02 PM   #6
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
It's always good when a questions answers are in agreement-- something that doesn't always happen!

Kind regards,


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing environment variables into bash scripts JDska55 Linux - Newbie 1 08-11-2009 04:27 PM
bash/ksh variables question tpe Programming 2 08-11-2005 09:39 AM
Question on Bash Scripts Magsol Linux - General 9 06-08-2005 07:36 PM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 09:57 AM
BASH variables evaluation question. Thetargos Programming 11 08-11-2003 04:11 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration