开发者

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开发者_JAVA技巧 of temp is ${temp}"

I hope this question is clear. THanks!


What about this?

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

Then in the console just type:

${temp}


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜