这是我需要的代码:
#!/bin/sh
x1="a1 a2"
x2="b1 b2"
list=SOMETHING
for x in "$list"
do
echo $x
done
我想要的输出:
a1 a2
b1 b2
问题是:SOMETHING应该是什么?我希望$list的行为和$@一样.
注意:我不能使用$IFS而且我无法评估整个循环.
最佳答案 这可能是你能得到的最接近的:
#!/bin/sh
x1="a1 a2"
x2="b1 b2"
set -- "$x1" "$x2"
for x in "$@"
do
# echo $x
echo "[${x}]" # proves that the lines are being printed separately
done
输出:
[a1 a2]
[b1 b2]
在Bash中,您可以使用数组:
#!/bin/bash
x1="a1 a2"
x2="b1 b2"
list=("$x1" "$x2")
for x in "${list[@]}"
do
# echo $x
echo "[${x}]" # proves that the lines are being printed separately
done
相同的输出.