正确使用报价

时间:2012-11-07 21:08:33

标签: bash

我想用bash来实现这个目标:

for i in {1..2}
do
for j in {1..2}
do
echo $i $j >> tmp.txt
done
done

cat tmp.txt
1 1
1 2
2 1
2 2

但是我想用引号内的echo变量这样做:

cmd="
for i in {1..2};
do for j in {1..2};
do echo \$i "\$j" '\$i' "'\$j'" \$i "'$j'" \'$i\' \"$j\" >> tmp.txt;
done;
done
"
eval $cmd

我似乎无法得到正确的报价。是否有可能实现我想要的目标?

谢谢!

1 个答案:

答案 0 :(得分:3)

只需使用单引号,并确保在每个done之前加上分号。

cmd='for i in {1..2}; do
for j in {1..2}; do
echo $i $j >> tmp.txt;
done;
done'
eval $cmd