LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch
User Name
Password
Linux From Scratch This Forum is for the discussion of LFS.
LFS is a project that provides you with the steps necessary to build your own custom Linux system.

Notices


Reply
  Search this Thread
Old 06-04-2010, 09:29 AM   #1
rwcooper
Member
 
Registered: Mar 2006
Posts: 123

Rep: Reputation: 20
Question What does ! do in ${!VARNAME}


In BLFS there are a few functions that contain lines like:

for DIR in ${!PATHVARIABLE} ; do

and

export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"

I can't seem to find anything that tells me what the ! does in these statements.

Thanks,
Randy
 
Old 06-04-2010, 09:36 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
In BASH, "!" is typically used for negation....but I'm not quite sure what it means in your context. Off to run some experiments.....
 
Old 06-04-2010, 09:38 AM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
It is a way of saying the value of the variable that is named the value of this variable.

Here is an example. Say you set the value of FOO to asdf, and you set the value of BAR to foo. You can get the value of FOO with ${!BAR}. Here it is in code w/ an example of why you would want to do it:

Code:
FOO1='This is fun'
FOO2='So is this'
FOO3='No fun here'

for BAR in FOO1 FOO2 FOO3 ; do
        echo ${!BAR}
done
HTH

Forrest
 
1 members found this post helpful.
Old 06-04-2010, 09:55 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
the value of the variable that is named the value of this variable
Arrgh!!!---instant headache....

Obviously not negation in this context...

From ABS**:
Quote:
Example 34−2. Indirect variable references − the new way
#!/bin/bash
# Indirect variable referencing.
# This has a few of the attributes of references in C++.
a=letter_of_alphabet
letter_of_alphabet=z
echo "a = $a" # Direct reference.
echo "Now a = ${!a}" # Indirect reference.
# The ${!variable} notation is greatly superior to the old "eval var1=\$$var2"
echo
t=table_cell_3
table_cell_3=24
echo "t = ${!t}" # t = 24
table_cell_3=387
echo "Value of t changed to ${!t}" # 387
**The Advanced Bash Scripting Guide (AKA the "boat anchor")---get it at http://tldp.org
 
Old 06-04-2010, 09:55 AM   #5
rwcooper
Member
 
Registered: Mar 2006
Posts: 123

Original Poster
Rep: Reputation: 20
Thumbs up

Forrest,

Thanks, that's exactly what I wanted to know.

Randy
 
Old 06-04-2010, 10:04 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Interesting! Could be used to approximate content-addressable-arrays And surely can be used to write obfuscated bash
 
Old 06-04-2010, 10:06 AM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Quote:
Originally Posted by pixellany View Post
Arrgh!!!---instant headache....
Yeah, but I couldn't think of a better way to say it with only one cup of coffee under my belt.

Forrest
 
Old 06-04-2010, 10:08 AM   #8
rwcooper
Member
 
Registered: Mar 2006
Posts: 123

Original Poster
Rep: Reputation: 20
Hi,

I just downloaded the Advanced Bash Scripting Guide from the Linux Documentation Project and the example is now in chapter 36 as 36-2.

Thanks for the pointer.

Randy
 
  


Reply



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
${varname%/*} The Grepper Programming 4 04-19-2002 02:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch

All times are GMT -5. The time now is 03:11 AM.

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
Open Source Consulting | Domain Registration