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 and shell scripting languages 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 08:28 PM
direction symbol in a variable as part of the command peterloo Shell Programming and Scripting 5 12-06-2007 07:25 PM
Repacing part of string with a variable divz Shell Programming and Scripting 4 07-19-2007 02:09 AM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 03:56 PM
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 12:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-25-2008
Smoker's Avatar
Smoker Smoker is offline
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.
  #2 (permalink)  
Old 01-25-2008
KittyWu KittyWu is offline
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
  #3 (permalink)  
Old 01-25-2008
Smoker's Avatar
Smoker Smoker is offline
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0