1

First of all I'm sorry for a probable bad title, but I don't even know what to call this.

I'm trying the following:

#!/bin/sh
VAR1="28-00000202070c"
VAR2="28-0000018776d3"
VAR3="28-0000033a6174"

for sensor in VAR1 VAR2 VAR3
do
 echo "$sensor:  $$sensor"
done

The expected output would be:

VAR1: 28-00000202070c

VAR2: 28-0000018776d3

VAR3: 28-0000033a6174

The real output is:

VAR1: 24038sensor

VAR2: 24038sensor

VAR3: 24038sensor

and the strange prefix number keeps growing...

VAR1: 24039sensor

VAR2: 24039sensor

VAR3: 24039sensor

...

I'd like to ask:

1) What are the correct terms/keywords that describe what I'm trying to do here

2) How to get to the expected output

Thanks, Joaoabs

1 Answer 1

3

This is something that sh does not support, while bash does.

The correct syntax you should use is:

echo "$sensor ${!sensor}"

Test

$ cat a
#!/bin/bash                 <----- note I changed /bin/sh to /bin/bash

VAR1="28-00000202070c"
VAR2="28-0000018776d3"
VAR3="28-0000033a6174"

for sensor in VAR1 VAR2 VAR3
do
 echo "$sensor ${!sensor}"
done

$ ./a
VAR1 28-00000202070c
VAR2 28-0000018776d3
VAR3 28-0000033a6174
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.