0

我想用 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

我似乎无法正确引用引号。有没有可能实现我想要的?

谢谢!

4

1 回答 1

3

只需使用单引号并确保在每个done.

cmd='for i in {1..2}; do
for j in {1..2}; do
echo $i $j >> tmp.txt;
done;
done'
eval $cmd
于 2012-11-07T21:16:11.227 回答