shell脚本字符串替换使用shell变量

时间:2011-10-02 17:05:11

标签: bash shell unix

这是我非常基本的脚本:

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

但是,$ {line}本身包含“temp of temp = $ {temp}”

的值

我希望我的脚本能够回应“temp的价值是你好”。我尝试过很多事情,甚至

echo `echo ${line}`

但每次只打印“temp的值是$ {temp}”

我希望这个问题很明确。谢谢!

2 个答案:

答案 0 :(得分:1)

这个怎么样?

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

然后在控制台中输入:

${temp}

答案 1 :(得分:1)

好吧,我有eval的解决方案,但实际上使用eval是mauvais ton。

$> 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
相关问题