LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2010, 02:51 PM   #1
yargk
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
quick variable question in a shell script


I'd like to use a variable as an index to call another variable

z24=".75"

x="24"

printf $"z$x"

I know the last line is wrong. I'd like it to print .75. How do I do this? Thanks.
 
Old 02-23-2010, 02:57 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You are looking for arrays. Assuming you are using bash:
Code:
z[24]=".75"
x=24
echo ${z[$x]}
Evo2.
 
Old 02-23-2010, 02:57 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
eval echo \$z${x}
`eval` evaluates all arguments on the cmdline, and then executes the result as one unified statement.

Check `man eval` for more details

Sasha
 
Old 02-23-2010, 02:59 PM   #4
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
This:
Code:
$ echo $z$(echo $x)
but what an ugly hack, I must say :-S
 
Old 02-23-2010, 03:09 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
HasC -- have you tested that? It doesn't work for me..

Sasha
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
conky question: passing conky-variable to shell-script zlin50 Linux - Software 2 12-29-2012 07:47 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
shell script question $variable in loop icecubeflower Linux - Newbie 2 03-31-2009 10:09 AM
Shell-script question about getting a number as a variable stormrider_may Programming 9 03-14-2006 09:18 AM
quick question about PATH variable anders_ant Linux - Newbie 4 11-05-2004 12:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration