LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 01: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, 01:57 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

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

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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, 01: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, 02:09 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

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

Sasha
 
  


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

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

All times are GMT -5. The time now is 01:12 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