LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-21-2011, 02:58 AM   #1
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Rep: Reputation: 18
substitute variable in bash


hi all,

Assume that i a having the following three lines in an executable file

#/bin/bash
a=Tue
Tue=1

When i give echo $a the value should be 1, how to do this. Your suggestions please.

Thanks in advance,
Dinesh.
 
Old 03-21-2011, 03:04 AM   #2
ZenKen
LQ Newbie
 
Registered: Mar 2011
Distribution: Personal use: Gentoo or Archlinux, Production environments: CentOS or Debian
Posts: 9

Rep: Reputation: 4
Dinesh,

I don't quite see what you're trying to do.

In the line a=Tue are you trying to set the variable $a to the content of variable $Tue? Because simply stating a=Tue will set $a to actually contain the string Tue, not the contents of the variable $Tue.

To set the contents of a to the variable $Tue, do this: a=$Tue. If you just want Tue to equal 1 and then a equal Tue, do this:
Tue=1
a=$Tue

- ZenKen
 
Old 03-21-2011, 03:12 AM   #3
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi ZenKen Thanks for your reply,

in my case

#/bin/bash
a=Tue
Tue=1

$a is not predefined variable it changes when the script runs every time. some times a=Mon,a=Tue,a=Wed and so on.
 
Old 03-21-2011, 03:20 AM   #4
Slackyman
Member
 
Registered: Mar 2011
Location: Rome - Italy
Distribution: Slackware 13.1
Posts: 347

Rep: Reputation: 44
You script should be like this:
Code:
#!/bin/bash
a=$tue
tue=1
but when you assign a value to $a in the first place if tue is empty so will be $a.
Assigning a value of a variable to a another one means to assign its current value (at runtime).

So if the script runs twice, the second time $a will contain the last value of $tue.
 
Old 03-21-2011, 03:21 AM   #5
ZenKen
LQ Newbie
 
Registered: Mar 2011
Distribution: Personal use: Gentoo or Archlinux, Production environments: CentOS or Debian
Posts: 9

Rep: Reputation: 4
Dinesh,

Oh OK, I see what you're doing.

I'm pretty sure that isn't possible, bash is just a simple scripting language. I'd recommend you try this in C.

EDIT: Slackyman, I think he wants to reference the variable $a to the variable $Tue. In other words, use the output of $a as a variable.

The only way of doing this is to use if statements, but that doesn't really solve your problem.

One way you might think this would be possible would be:
echo $(echo \$$a)
But this just outputs "$tue" not the contents of it. Limitation of bash, I guess.

- ZenKen

Last edited by ZenKen; 03-21-2011 at 03:39 AM.
 
Old 03-21-2011, 03:47 AM   #6
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
Hi dinesh ,


Try this

Code:
eval echo \$$a
 
1 members found this post helpful.
Old 03-21-2011, 03:52 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
This is called Indirect Variable Reference. You can try the solution suggested by anishkumarv or use the new notation:
Code:
echo ${!a}
 
1 members found this post helpful.
Old 03-21-2011, 04:44 AM   #8
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
Hi colucix,


Compare to my code...This is better opt!!! try this dinesh
 
Old 03-21-2011, 04:44 AM   #9
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi all,

Thanks all for your suggestions. It works.
 
  


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
bash: substitute parameter in a quoted string, stored in another variable GamerX Linux - General 5 01-31-2010 09:48 PM
BASH: how to substitute a expression with another without creating a new file carolflb Linux - Newbie 8 11-18-2009 10:42 AM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Linux - Newbie 6 10-20-2009 02:28 PM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Programming 2 10-20-2009 11:21 AM


All times are GMT -5. The time now is 08:17 AM.

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