LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-06-2008, 11:32 PM   #1
coolhandluke1
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Rep: Reputation: 0
variable expansion in bash


Im reading a book on shell scripting and an example is given:

***FULL EXAMPLE***
# $envvar = PATH
dirpath=$(eval echo '${'"$envvar"'}' 2>/dev/null | tr : ' ')


***SPECIFIC PART I DONT UNDERSTAND***
# $envvar = PATH
echo '${'"$envvar"'}'

this prints:
${PATH}


What i am needing help understanding is:

how does the argument string '${'"$envvar"'}' get converted to ${PATH}? I know it has something to do with evaluation order of the shell, however the books explanation of this subject is not detailed enough for me.

Thanks in advance
 
Old 01-07-2008, 03:21 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by coolhandluke1 View Post
how does the argument string '${'"$envvar"'}' get converted to ${PATH}? I know it has something to do with evaluation order of the shell, however the books explanation of this subject is not detailed enough for me.
The shell variable envvar has the value 'PATH'. So $envvar is substituted ("expanded" in shell lingo) with PATH.

'${' and '}' are just a literal strings. The single quotes prevent the shell from treating the $ and the { as special character and trying to interpret them.

The double quotes prevents whitespace to be interpreted as seperators. And since PATH does not contain spaces, the double quotes are not really needed in this case. So this would also do it:
Code:
dirpath=$(eval echo '${'$envvar'}' 2>/dev/null | tr : ' ')
BTW, $PATH does the same as ${PATH}. Only in some cases the curly brackets are needed. Not here. So this command would do the same (in this case):
Code:
dirpath=$(eval echo '$'$envvar 2>/dev/null | tr : ' ')
So a string ${PATH} is created by expanding the envvar variable and we want that string expanded yet another time by the shell. This is done by the eval command. This is some sort of trick to get the value of a variable, while the name of the variable itself is inside a variable. The bash shell has a special construction to do this. So if the shell is bash, this does the same as you original full example:
Code:
dirpath=$(echo ${!envvar} | tr : ' ')

Last edited by Hko; 01-07-2008 at 03:22 AM.
 
Old 01-07-2008, 03:27 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,752

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
Looks like a slight typo to me. Try it like this:

Code:
#!/bin/bash
# save me as 'tryit.sh'
envvar=PATH
dirpath=$(eval echo '${'"$envvar"'}' 2>/dev/null | tr : ' ')
echo $dirpath
Then run it:
sh ./tryit.sh
/usr/local/bin /usr/bin /bin /usr/X11R6/bin /usr/games /usr/lib/java/bin /usr/lib/java/jre/bin /usr/lib/qt/bin .

You should use the CODE tags when posting code so we can read it accurately.
 
Old 01-07-2008, 05:35 AM   #4
coolhandluke1
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hko View Post
The shell variable envvar has the value 'PATH'. So $envvar is substituted ("expanded" in shell lingo) with PATH.

'${' and '}' are just a literal strings. The single quotes prevent the shell from treating the $ and the { as special character and trying to interpret them.
Thank you for your initial reply. This answers my question right here.

Last edited by coolhandluke1; 01-07-2008 at 05:36 AM.
 
Old 01-09-2008, 03:45 PM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
The whole sequence prints the contents of the Environment Variable "PATH" w/ the colons changed to spaces. For some reason, it's using indirection (the value of the variable envvar, is the name of the variable PATH; & so the indirect value of envvar is the value of PATH). Why the author did not just use straight bash indirect expansion, I don't know. The following produces the same result:
Code:
envvar=PATH
dirpath=`echo ${!envvar} 2>/dev/null |tr : \ `
 
  


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
Variable expansion in BASH champak Programming 5 11-26-2007 02:44 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 11:45 AM
Variable expansion inside of a bash script! A.S.Q. Linux - Newbie 4 09-29-2006 09:09 AM
bash script $n variable expansion cortez Programming 6 12-08-2003 04:03 PM


All times are GMT -5. The time now is 08: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