LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 11-19-2009, 09:29 PM   #1
gimpy530
Member
 
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`

and

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?

Code:
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
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS6
Posts: 267

Rep: Reputation: 57
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
urban.yoga.journeys
Member
 
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
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
http://mywiki.wooledge.org/BashFAQ/082

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.

Sasha
 
Old 11-19-2009, 09:53 PM   #5
gimpy530
Member
 
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
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

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

Kind regards,
Sasha
 
  


Reply


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


All times are GMT -5. The time now is 03:52 PM.

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