LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-16-2008, 04:54 AM   #1
SwingingSimian
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Rep: Reputation: 0
BASH variable referencing


Hi all

Another no brainer(I must have left mine at home today). I'm sure I have done this before but I can't figure it out.

This is also very similar to one of my previous posts about indirect referencing, but slightly different.

I want to set a varaible such that it updates it's value with reference to another variable. Much like aliasing can do with single quotes.

e.g.

>bar=test
>foo=$bar

>echo $foo
>test

>bar=test1
>echo $foo
>test1

Indirect referencing isn't quite what I want for this, but I'm sure there is something very obvious that I've missed.

Thanks

Nath
 
Old 10-16-2008, 05:03 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
See indirect expansion in man bash:

Code:
$ bar=test    
$ foo=bar     
$ echo ${!foo}
test
 
Old 10-16-2008, 05:11 AM   #3
SwingingSimian
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
I've already looked at this and it doesn't do quite what I want as it forces me to use the ${!foo} notation. As I am potentially directly setting foo to another value, I want this to work by simply using "$foo".

Thanks

Nath
 
Old 10-16-2008, 05:20 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Your example shows foo as being as being the thing you dereference, and it points to bar. And bar contains a value. Can you show an example of something that doesn't follow this classic indirection problem example?
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash and ip variable skids48 Linux - Networking 2 02-15-2008 07:02 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
Variable with bash pinkysioux Programming 6 04-15-2004 08:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 PM.

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