LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Basic questions re: bash (https://www.linuxquestions.org/questions/linux-newbie-8/basic-questions-re-bash-834584/)

veeruk101 09-26-2010 10:13 AM

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.

colucix 09-26-2010 10:38 AM

Quote:

Originally Posted by veeruk101 (Post 4109474)
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

fuubar2003 09-26-2010 10:42 AM

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 ~ $



All times are GMT -5. The time now is 02:16 PM.