Operating System - HP-UX
1753479 Members
4842 Online
108794 Solutions
New Discussion юеВ

Re: what does the mean about $

 
SOLVED
Go to solution
Rambo_1
Regular Advisor

what does the mean about $

Hi,
I often see the symbol "$" in some script:
PATH=${ORACLE_HOME}/bin:/sbin:/usr/bin:/usr/sbin:/etc:/bin
what does the mean and what function about the $ ?

thanks for your help
4 REPLIES 4
Stf
Esteemed Contributor

Re: what does the mean about $

$ is used in scripting to call a variable...

Stf ;-)
Gopi Sekar
Honored Contributor

Re: what does the mean about $



$ is a variable name identification used in shell.

in shell: just VAR means list of characters. $VAR means variable

if you say echo VAR, it will just print VAR. whereas if you say echo $VAR, it will print the contents of the variable VAR.

I guess i explained more than enough



Never Never Never Giveup
Tim D Fulford
Honored Contributor
Solution

Re: what does the mean about $

All of the above 100% correct. But it goes a little deeper, and the $ prefix is more functional than has been explained.

1 - variable concatination.
${ORACLE_HOME} this will evaluate that variable, say /home/oracle
${ORACLE_HOME}XXX This will evaluate ${ORACLE_HOME} & ADD XXX so, /home/oracleXXXX.
${ORACLE_HOME}/${MY_SCRIPTS} would concatanate the whole string to say, /home/Oracle/scripts/bin (assuming MY_SCRIPTS=scripts/bin).

The reason for the curly brackets ${} is to explicitly define the variable. e.g. you COULD have used $ORACLE_HOMEXXX . this would NOT work as there is no (well it is unlikley) to be a varable called "ORACLE_HOMEXXX". The backets allow explicit variable concatination.

2 - variable substitution.
Sometimes you want to read the result of a command into a variable e.g.
$TDAY --> todays date.
This can be done like so
export TDAY = $(date)

Now $TDAY has todays date in it. In effect the $(..cmd..) executes the command and presents it to the shell as a variable, (as opposed to standard error or standard out) so the following is equally valid (but trivial)
echo $(date)

I fully admit that the above is trivial, but it can be useful in more complex scripts. Imagine that you wanted to print out all the logical volumes you have in vg01. You can list all the logical volume names using vgdisplay -v vg01|grep "LV Name"; but this is not enough as you want ALL the info for each LV (without doing each one manually)... so

for lv in $( /etc/vgdispalay -v vg01| perl -ane 'if (m/LV Name/) { print "$F[2] "})
do
/etc/lvdisplay $lv
done

IF you were not allowed to use the structure $( ..cmd.. ) then the only other way of doing this would be to output the above commands to a file & read it back into a for;do;done loop. This would be slower as it needs to write the data to disks as opposed to doing the whole operation in memory.

Anyway.. the $VAR; ${VAR}; and $( ..cmd.. ) structures are very useful..

Regards

Tim
-
Sandman!
Honored Contributor

Re: what does the mean about $

The "$" symbol is used for variable expansion. For example you can set any variable (say ORACLE_HOME) to a string.

# ORACLE_HOME=/opt/oracle/9.2.0

now use this value in other variables or simply echo it at the command line

#PATH=$ORACLE_HOME/bin:/sbin:/usr/sbin:/etc:/bin

#echo $PATH
/opt/oracle/9.2.0/bin:/sbin:/usr/sbin:/etc:/bin

cheers!