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"

5
  • 3
    12 is not a variable, and there's no reason why it should map automatically? Commented Sep 22, 2014 at 16:30
  • can't 12 be a variable? I'm only using it as an example. What if we changed 12 to XY Commented Sep 22, 2014 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. Commented Sep 22, 2014 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? Commented Sep 22, 2014 at 16:42
  • 1
    possible duplicate of Bash dynamic variable names Commented Sep 22, 2014 at 16:43

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply but this is not what I am trying to do. I'm trying to call a varible by using varibles
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}".
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

2 Comments

No that's no what I am trying to do. I'm just trying to call $12 by creating the variable name from two other variables
I think you need a dynamic variable, check the answer now.
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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.