使用 ksh 我使用 read 作为分隔值的便捷方法:
$ echo 1 2 3 4 5 | read a b dump
$ echo $b $a
2 1
$
Run Code Online (Sandbox Code Playgroud)
但它在 Bash 中失败了:
$ echo 1 2 3 4 5 | read a b dump
$ echo $b $a
$
Run Code Online (Sandbox Code Playgroud)
我没有在手册页中找到失败的原因,知道吗?
为什么我$x
从下面的片段中得到不同的值?
#!/bin/bash
x=1
echo fred > junk ; while read var ; do x=55 ; done < junk
echo x=$x
# x=55 .. I'd expect this result
x=1
cat junk | while read var ; do x=55 ; done
echo x=$x
# x=1 .. but why?
x=1
echo fred | while read var ; do x=55 ; done
echo x=$x
# x=1 .. but why?
Run Code Online (Sandbox Code Playgroud)