The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 05:28 PM
direction symbol in a variable as part of the command peterloo Shell Programming and Scripting 5 12-06-2007 04:25 PM
Repacing part of string with a variable divz Shell Programming and Scripting 4 07-18-2007 10:09 PM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 11:56 AM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 08:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-25-2008
Smoker's Avatar
Registered User
 

Join Date: May 2005
Posts: 12
variable part of variable name

Greetings

I am having a hard time trying to figure out how to use a variable as part of another variables name.

For instance:

A="PRE"
eval ${A}_DAY=`date +%d`
echo ${A}_DAY
echo $PRE_DAY

output:
PRE_DAY
25

I would appreciate some help with this.
Reply With Quote
Forum Sponsor
  #2  
Old 01-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 8
Hi,

I'm not sure this answer suits to your request (if you meant
"how to display/store or use the resulting value", then this
should help...).

First, the 2 first lines sound correct for both defining a variable
and assigning a value to it:
me@host> A="PRE"
me@host> eval ${A}_DAY=`date +%d`

In order to solve this kind of trouble, I usually use this
kind of syntax:
me@host> DAY_VALUE=$(unalias echo; eval echo $"${A}_DAY")
me@host> echo ${DAY_VALUE}
me@host> 25

Hope it helps,
Christophe
Reply With Quote
  #3  
Old 01-25-2008
Smoker's Avatar
Registered User
 

Join Date: May 2005
Posts: 12
Quote:
Originally Posted by KittyWu View Post
Hi,

I'm not sure this answer suits to your request (if you meant
"how to display/store or use the resulting value", then this
should help...).

First, the 2 first lines sound correct for both defining a variable
and assigning a value to it:
me@host> A="PRE"
me@host> eval ${A}_DAY=`date +%d`

In order to solve this kind of trouble, I usually use this
kind of syntax:
me@host> DAY_VALUE=$(unalias echo; eval echo $"${A}_DAY")
me@host> echo ${DAY_VALUE}
me@host> 25

Hope it helps,
Christophe
Ran into a couple of other ways...

echo $(eval echo \$${A}_DAY)
eval echo "\$${A}_DAY"

Thank You for your answer
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0