1

I have a variable that has a nested value.

x=hello
y=world
helloworld=monday

TEMP=$x$y 
echo "${!TEMP}"   # I get output "monday" which is required

This works in bash but not in plain sh. When I run same command using sh the last line gives "Bad Substitution".

How can I get same result using sh? For some reasons I cannot change shell from sh to bash, so I have to do this with sh.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Rezwan
  • 1,203
  • 1
  • 7
  • 22
  • Indirect variable name references are a major code smell. [What are you trying to do?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) There's sure to be a better way. – John Kugelman Jul 16 '19 at 12:55
  • Indeed POSIX shells don't have variable indirection and I second @johnkugelman 's comment about code smell. – Léa Gris Jul 16 '19 at 12:57
  • I have two variables like x and y, when I combine their values they form another variable just as I mentioned above, Now I want to get value of that variable that came from value of x and y.... I dont think I am explaining it well. :( – Rezwan Jul 16 '19 at 13:04
  • Is there any better way to achieve the same target? coz in bash I can do that then in sh there must be a way – Rezwan Jul 16 '19 at 13:06
  • If you have code like this, it's a strong indication that it's time to move to another programming language. – chepner Jul 16 '19 at 13:27
  • No need for that @chepner, I figured it out. I needed to use eval which helped me in constructing a command. that i was able to run in even bash. – Rezwan Jul 16 '19 at 13:40
  • Thanks @JohnKugelman and Lea for providing the concept, as I am very new to all this :D – Rezwan Jul 16 '19 at 13:42
  • 1
    No no, don't use eval. If you could take a step back and tell us what your larger goal is in your code, we can tell you a better way to achieve it. Read the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) page I linked in my first comment. You're asking about your attempted *solution* and we want to know what your *actual problem* is. "You are trying to solve problem X, and you think solution Y would work, but instead of asking about X when you run into trouble, you ask about Y." – John Kugelman Jul 16 '19 at 13:44
  • I am not very good at explaining problems, maybe this can help, I am getting issue somewhat like this. https://unix.stackexchange.com/questions/87941/how-achieve-variable-indirection-refer-to-a-variable-whose-name-is-stored-in-an – Rezwan Jul 16 '19 at 13:46
  • @Rezwan You're still asking about solution Y instead of problem X. **Why** do you want indirect variable references? What is the **problem** you're trying to *solve* with them? – John Kugelman Jul 17 '19 at 00:28

1 Answers1

0

As sh is different than bash difference-between-sh-and-bash. But you can get the content of helloworld using this way, maybe there is some better approach but if you're interested in just solution then this will give you what you are looking for :)

#!/bin/sh
x=hello
y=world
helloworld=monday
TEMP=$x$y
TEMP_FINAL='eval "echo \$$TEMP"'
eval "$TEMP_FINAL"

enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148