这是我需要的代码:
#!/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
,也不能使用eval
整个循环。
这可能是你能得到的最接近的:
#!/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
相同的输出。
It is not possible in standard POSIX shell.