1

I have the following code

let a=1
let b=a

echo $b

When i echo b, i want it to refer it's value (which is 'a'), and display the value of a( which is '1')

Can this be achieved?

1

3 Answers 3

2

First, I would argue you don't really want to do this. But instead of convincing you, here's how to do it:

$ let a=1
$ b=a
$ echo $b
a
$ eval "echo \$$b"
1

note that you can't use "let" for the second assignment since you want to access the right-hand-side as a string later.

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

Comments

1

I think you want let b=$a

1 Comment

Thanks for your reply Sharet. In my case that is not the scenario. I just have a string, which may be 'a1', 'a2' or 'a3'. I get this particular string from sql server. I have predefined variables a1, a2 and a3. I just have to access the corresponding variable for the string.
0

If your shell is bash, then what you wrote actually sets b to "1" (not "a") because the $ is optional in arithmetic expressions (which you force with the use of let).

Without using let, this is the syntax you're looking for (assuming bash): a=1; b=a; echo $b ${!b} which outputs a 1

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.