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
Variable substitution Leo_NN UNIX for Dummies Questions & Answers 7 10-17-2008 12:24 PM
Sed variable substitution when variable constructed of a directory path alrinno Shell Programming and Scripting 2 07-11-2008 03:24 PM
KSH variable substitution tipsy Shell Programming and Scripting 5 08-14-2006 06:07 PM
awk variable substitution apalex UNIX for Dummies Questions & Answers 1 09-10-2004 07:02 PM
Substitution in a variable spragueg UNIX for Advanced & Expert Users 3 10-18-2001 10:14 AM

Reply
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 06-11-2009
aoussenko aoussenko is offline
Registered User
  
 

Join Date: May 2008
Posts: 119
variable substitution in ksh

Hi
I have a variable BIT1 which holds some value. Is there a way to retrieve the value of this variable indirectly via another variable, lets say SUBSET_BIT_NUM=1, so the call will look something like this:

sundev1 $ echo ${BIT${SUBSET_BIT_NUM}}
ksh: ${BIT${SUBSET_BIT_NUM}}: bad substitution

Thanks a lot for any advice -A
  #2 (permalink)  
Old 06-11-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,125
Code:
#!/bin/ksh

BIT1=aoussenko
BIT2=foo

SUBSET_BIT_NUM=1

eval echo \${BIT${SUBSET_BIT_NUM}}
  #3 (permalink)  
Old 06-11-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,930
The following also works
Code:
eval echo '${BIT'${SUBSET_BIT_NUM}'}'
  #4 (permalink)  
Old 06-11-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by fpmurphy View Post
The following also works
Code:
eval echo '${BIT'${SUBSET_BIT_NUM}'}'

That's not very reliable:

Code:
  BITTER=qwerty
  SUBSET_BIT_NUM=TER
  IFS=E
  eval echo '$BIT'$SUBSET_BIT_NUM  ## unreliable
  eval echo "\$BIT$SUBSET_BIT_NUM" ## correct
The output is:

Code:
R
qwerty
Reply

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 08:44 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