Indirect variable assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Indirect variable assignment
# 1  
Old 10-04-2010
Indirect variable assignment

Hi

I have variable A_B=alpha
also var1="A"
var2="B"

I want to retrieve the value alpha using var1 and var2 , somthing like
echo ${${var1}_${var2}} that works. Obviously this is receiving syntax
error
# 2  
Old 10-04-2010
Use this:

Code:
$ A_B=alpha
$ var1="A"
$ var2="B"
$ eval echo \${${var1}_${var2}}
alpha

# 3  
Old 10-04-2010
try with eval:

Code:
eval echo \${${var1}_${var2}}

# 4  
Old 10-04-2010
Code:
V=${var1}_${var2}
$ echo ${!V}

# 5  
Old 10-04-2010
Quote:
Originally Posted by frans
Code:
V=${var1}_${var2}
$ echo ${!V}

In which shell should this work? I tried it with
  • bash ... empty output
  • ksh ... bad substitution
  • zsh ... prints "A_B"
Smilie
# 6  
Old 10-04-2010
Quote:
Originally Posted by hergp
In which shell should this work?
Smilie
Works in bash from version 2
# 7  
Old 10-04-2010
Strange, it works now, but it did not work the first time I tried (empty output). Must have mistyped something.

Anyway, thanks for the clarification.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

3. Shell Programming and Scripting

Usage of # in assignment of variable

guys, i would like to know what does the below does. tr=`echo $bfile | cut -d"." -f4` tr=${tr#TR} i tried with assigning a value and executed second line. but after that also value of tr remains same. thanks in advance .. (1 Reply)
Discussion started by: expert
1 Replies

4. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

5. Shell Programming and Scripting

substituted variable assignment

I try to run this script, however, it gives an exception in line 3. How do I do an assignment to a substituted variable? #!/bin/bash name=fruit ext_$(eval echo ${name})=apple tmp=ext_$(eval echo ${name}) if ]; then echo "apple" elif ]; then echo "orange" fi echo ${!tmp} Error... (2 Replies)
Discussion started by: angelokh
2 Replies

6. Linux

How to get an Indirect Variable Value..?

Hi, I've got a small problem. If varible A stores "B" and Variable B stores C, How to get the value of variable B by using only Variable A..? I tried the following but didnt work pease help.. $ var1=vikram $ echo $var1 vikram $ vikram=sampath $ echo $vikram sampath $ echo... (6 Replies)
Discussion started by: vickramshetty
6 Replies

7. Shell Programming and Scripting

Length of an indirect variable

The construct ${#parameter} returns the number of characters in the parameter and ${!parameter} specifies an indirect variable. My question is: How do I combine these two. What I want is ${#!parameter} but this gives an error. Of course I can use: dummy=${!parameter} ${#dummy} but that's a... (0 Replies)
Discussion started by: gone_bush
0 Replies

8. Shell Programming and Scripting

variable assignment in new shell

Hi, I'm writing a KSH script, and at one point, I have to call a new shell and perform some variable assignments. I noticed that the assignment is not working. Please see two samples below: Command 1: #>ksh "i=2;echo I is $i" Output: #>I is Command 2: #>ksh <<EOF > i=2 > echo I... (7 Replies)
Discussion started by: maverick80
7 Replies

9. UNIX for Advanced & Expert Users

Compound indirect variable references

Using bash, I'm trying to read a .properties file (name=value pairs), assigning an indirect variable reference for each line in the file. The trick is that a property's value string may contain the name of a property that occurred earlier in the file, and I want the name of the 1st property to... (5 Replies)
Discussion started by: tkrussel
5 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question