Bill
Bill

Reputation: 2349

shell scripting string replace using shell variables

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!

Upvotes: 1

Views: 278

Answers (2)

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

Upvotes: 1

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

What about this?

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

Then in the console just type:

${temp}

Upvotes: 1

Related Questions