LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2009, 09:49 PM   #1
rabbit2345
Member
 
Registered: Apr 2007
Location: SC
Distribution: Kubuntu 20.04 LTS
Posts: 378

Rep: Reputation: 41
how to make bash replace the value of a variable?


Hi,

I have a program I am writing where I have a for loop and I want to make it substitute the variable twice like:

for ((i=0;i<5;i++)) do
echo $"$i"
done

I wanted it to replace $i with a value, and then echo the value of the variable named from that output. How can you tell bash to do a double substitution?

Like:
echo $"$i"

echo $3

echo [text from $3]


Thanks,
rabbit2345
 
Old 04-18-2009, 10:26 PM   #2
rriggs
Member
 
Registered: Mar 2009
Location: Colorado, US
Distribution: Fedora 13, Fedora 14, RHEL6 Beta
Posts: 46

Rep: Reputation: 17
http://www.enricozini.org/2008/tips/...direction.html
 
Old 04-18-2009, 10:33 PM   #3
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
I'm not sure how to do exactly what you're asking, but I think that you could get the same effect using an array

Code:
#! /bin/bash
declare MY_ARRAY=("foo" "bar" "baz" "quux" "quuux")

for ((i=0;i<5;i++)) do
echo ${MY_ARRAY[$i]}
done
 
Old 04-18-2009, 11:02 PM   #4
rabbit2345
Member
 
Registered: Apr 2007
Location: SC
Distribution: Kubuntu 20.04 LTS
Posts: 378

Original Poster
Rep: Reputation: 41
thanks rriggs for your reply. It worked out exactly the way I needed it.

Thanks,
rabbit2345
 
  


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
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
how to replace with variable using sed? babag Programming 4 09-17-2008 03:28 PM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 01:14 PM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 09:29 AM
bash script to replace char in variable gmartin Programming 5 11-08-2004 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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