Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

substitute variable in bash

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-21-2011   -   Original Discussion by anishkumarv
anishkumarv's Unix or Linux Image
anishkumarv anishkumarv is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 14 May 2013, 12:44 AM EDT
Posts: 96
Thanks: 15
Thanked 2 Times in 2 Posts
substitute variable in bash

hi all,

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

Code:
#/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,
Anish

Last edited by Franklin52; 03-21-2011 at 04:56 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 03-21-2011   -   Original Discussion by anishkumarv
theboogymaster's Unix or Linux Image
theboogymaster theboogymaster is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 21 September 2017, 12:05 PM EDT
Posts: 56
Thanks: 7
Thanked 7 Times in 7 Posts
You should initiate the Tue Variable before the a variable.

In your script, when you assign the Tue variable to a it has no value.
Your script should be like this :

Code:
#/bin/bash
Tue=1
a=Tue

Sponsored Links
    #3  
Old Unix and Linux 03-21-2011   -   Original Discussion by anishkumarv
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 22 September 2016, 3:57 AM EDT
Location: IN
Posts: 894
Thanks: 15
Thanked 183 Times in 174 Posts
And since variable 'a' does not hold value as $Tue we add another $ in the below echo statement to print Tue's value.

Code:
eval echo \$$a

    #4  
Old Unix and Linux 03-21-2011   -   Original Discussion by anishkumarv
anishkumarv's Unix or Linux Image
anishkumarv anishkumarv is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 14 May 2013, 12:44 AM EDT
Posts: 96
Thanks: 15
Thanked 2 Times in 2 Posts
Hi dude ,

The code which you posted is not working

echo $a given means it shows tue
Sponsored Links
    #5  
Old Unix and Linux 03-21-2011   -   Original Discussion by anishkumarv
frans's Unix or Linux Image
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 1 February 2016, 9:47 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts
Quote:
Originally Posted by theboogymaster View Post
You should initiate the Tue Variable before the a variable.

Code:
#/bin/bash
Tue=1
a=Tue

But:
Code:
#/bin/bash
Tue=1
a=${!Tue}

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
substitute variable for values in perl cesarNZ Shell Programming and Scripting 1 01-24-2011 08:22 PM
sed doubt - search and substitute string from variable. dragon.1431 Shell Programming and Scripting 9 09-24-2010 09:14 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
Substitute Variable mmg2711 UNIX for Dummies Questions & Answers 0 10-26-2007 07:59 AM
Substitute variable values John Rihn Shell Programming and Scripting 12 03-15-2006 10:40 PM



All times are GMT -4. The time now is 10:20 PM.