Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Showing results for 
Search instead for 
Did you mean: 

what does the mean about $

Go to solution
Regular Advisor

what does the mean about $

I often see the symbol "$" in some script:
what does the mean and what function about the $ ?

thanks for your help
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

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] "})
/etc/lvdisplay $lv

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..


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


#echo $PATH