LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2010, 11:13 AM   #1
veeruk101
Member
 
Registered: Mar 2005
Distribution: Ubuntu 12.04 LTS
Posts: 249

Rep: Reputation: 16
Basic questions re: bash


I've got a few pretty basic questions about bash. I came across the following link (http://trac.edgewall.org/ticket/4352) which has some variables listed as:

Code:
tracd=${TRACD-/usr/sbin/tracd}
pidfile=${PIDFILE-/var/run/tracd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/tracd}
1) My understanding is the script is just using the value of the path within the curly braces, so I'm not sure what 'TRACD-' in the first line, for example, is doing - is it some special syntax?

2) When you assign a variable in a script as myvar=whatever, what's the difference between referring to $myvar and ${myvar} - what do the curly braces do?

3) When you assign a variable on the command line like myvar=whatever, what's the scope or lifetime of that 'myvar' variable? Is it for the lifetime of the shell, for all active logins under that shell, does it get saved somewhere?

Thanks a lot.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-26-2010, 11:38 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by veeruk101 View Post
1) My understanding is the script is just using the value of the path within the curly braces, so I'm not sure what 'TRACD-' in the first line, for example, is doing - is it some special syntax?
This matches a rule of parameter substitution: if the variable TRACD is not set, then it evaluates as /usr/sbin/tracd otherwise it retains its value. In practice this means: assign to tracd the value of the TRACD variable if it's set, otherwise assign the string /usr/sbin/tracd.

Quote:
2) When you assign a variable in a script as myvar=whatever, what's the difference between referring to $myvar and ${myvar} - what do the curly braces do?
No difference in this case. The curly braces are the right way to evaluate shell variables, but you can omit them in many case. On the contrary, suppose you have a variable named var and you want to embed its value in a string like:
Code:
this_is_a_string_containing_the_$var_value
In this case you're forced to use the braces, otherwise the shell tries to evaluate a variable whose name is var_value (remember the underscore is a valid character for variable names).
Code:
this_is_a_string_containing_the_${var}_value
Quote:
3) When you assign a variable on the command line like myvar=whatever, what's the scope or lifetime of that 'myvar' variable? Is it for the lifetime of the shell, for all active logins under that shell, does it get saved somewhere?
The scope is local to the shell where the variable has been set (assigned). On the command line you loose the variable when you quit that shell session and from a script the value is lost when the script terminates. No other newly or already open shell sees that value.

Anyway, check the export statement to see how variables can be inherited from the parent shell. Suppose you have a script (parent) which launches another script (child). If you export a variable in the parent script, its value is available to the child. This is the way by which environment variables like HOME and PATH are made available to all the scripts executed from the shell.

Hope this helps. Good references to check:
http://www.gnu.org/software/bash/manual/bashref.html
http://www.tldp.org/LDP/abs/html/index.html
 
2 members found this post helpful.
Old 09-26-2010, 11:42 AM   #3
fuubar2003
Member
 
Registered: May 2004
Location: Orlando, Florida
Distribution: SLES10/11, RH4/5 svrs, Fedora, Debian/Ubuntu/Mint; FreeBSD/OpenBSD
Posts: 63

Rep: Reputation: 26
You can also see what it does by executing it, as I have done here:

Code:
craptop3 ~ $ pidfile=${PIDFILE-/var/run/tracd.pid}
craptop3 ~ $ echo $pidfile
/var/run/tracd.pid
craptop3 ~ $
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GnuCash, KMyMoney - basic, basic questions - please help if you can. brjoon1021 Linux - Software 3 10-20-2008 07:27 PM
a few basic questions alias59 Fedora 3 06-06-2008 01:52 PM
basic questions on hostname and domain name + related postfix questions Moebius Linux - Newbie 7 09-04-2007 12:50 PM
A Few Basic Questions philipogletree Linux - Newbie 4 07-21-2005 10:51 PM
Basic Questions IntelSlacker Slackware 5 03-26-2005 11:47 PM


All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration