0

Hi I would like to set a variable by concatenating two other variables.

Example

A=1
B=2
12=C

echo $A$B

desired result being C

however the answer I get is always 12

Is it possible?

UPDATED Example

A=X
B=Y
D=$A$B

xy=test

echo $D

desired result being "test"

user3246938
  • 89
  • 2
  • 12
  • 3
    12 is not a variable, and there's no reason why it should map automatically? – Pieter21 Sep 22 '14 at 16:30
  • can't 12 be a variable? I'm only using it as an example. What if we changed 12 to XY – user3246938 Sep 22 '14 at 16:40
  • To be clearer (and maybe get a better answer), I suggest you change the name of this question to "Look up bash variable name by value", which is the more difficult part of what you're trying to do. – Jeff Bowman Sep 22 '14 at 16:41
  • I don't think you can look up a variable by value that easy. Suppose both 'C' and 'XY' have value 12, and you'd want to lookup 12. What would be the desired outcome? Any one? A collection? The most recent one? – Pieter21 Sep 22 '14 at 16:42
  • 1
    possible duplicate of [Bash dynamic variable names](http://stackoverflow.com/questions/16553089/bash-dynamic-variable-names) – legends2k Sep 22 '14 at 16:43

3 Answers3

2

Since 12 is not a valid variable name, here's an example with string variables:

> a='hello'
> b='world'
> declare my_$a_$b='my string'
> echo $my_hello_world
my string
legends2k
  • 31,634
  • 25
  • 118
  • 222
2

It looks like you want indirect variable references.

BASH allows you to expand a parameter indirectly -- that is, one variable may contain the name of another variable:

# Bash
realvariable=contents
ref=realvariable
echo "${!ref}"   # prints the contents of the real variable

But as Pieter21 indicates in his comment 12 is not a valid variable name.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Thanks for the reply but this is not what I am trying to do. I'm trying to call a varible by using varibles – user3246938 Sep 22 '14 at 16:43
  • Yes, that's what this does. It lets you construct a variable whose contents are the name of another variable to get the value from. Try `realvariable=contents; a=real; b=variable; ref=$a$b; echo "${!ref}"`. – Etan Reisner Sep 22 '14 at 16:48
1

What you are trying to do is (almost) called indirection: http://wiki.bash-hackers.org/syntax/pe#indirection

...I did some quick tests, but it does not seem logical to do this without a third variable - you cannot do concatenated indirection directly as the variables/parts being concatenated do not evaluate to the result on their own - you would have to do another evaluation. I think concatenating them first might be the easiest. That said, there is a chance you could rethink what you're doing. Oh, and you cannot use numbers (alone or as the starting character) for variable names.

Here we go:

cake="cheese"

var1="ca"
var2="ke"

# this does not work as the indirection sees "ca" and "ke", not "cake". No output.
echo ${!var1}${!var2}

# there might be some other ways of tricking it to do this, but they don't look to sensible as indirection probably needs to work on a real variable.
# ...this works, though:
var3=${var1}${var2}
echo ${!var3}
bryn
  • 3,155
  • 1
  • 16
  • 15