1

This is my very basic script:

  temp=hello 
  while read line;
  do
     echo ${line}
  done

However, ${line} will itself consist of "value of temp = ${temp}"

I want my script to echo "value of temp is hello". I've tried doing many things, even

echo `echo ${line}`

but each time it always just prints "value of temp is ${temp}"

I hope this question is clear. THanks!

2 Answers 2

1

What about this?

temp=hello 
while read line; do
    echo "Value of temp = `eval echo ${line}`"
done

Then in the console just type:

${temp}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate. That seems to work, what I did was replace the middle line with simply: eval echo "${line}"... Thank! (I'd upvote but don't have the karma apparently)
1

Well, I have solutions with eval, but using eval is mauvais ton, actually.

$> cat 7627845.sh 
#!/bin/bash

temp=hello 
cat file_with_values.log | while read line;
do
    eval echo "${line}"
done


$> cat file_with_values.log 
value of temp = ${temp}

$> ./7627845.sh 
value of temp = hello

3 Comments

useless use of cat: while read line; do eval echo "$line"; done < file_with_values.log
Frankly speaking there is no huge difference between those methods.
depends what you're doing in the while body. If you cat file | while ... then the while command runs in a subshell, and any variables you set in the while body disappear when the subshell ends. So, potentially, there could be a huge difference. Obviously, that's not the case here, but this is a teaching site, so it's better to teach good principles.

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.