Pra*_*al 0 bash shell stdout stderr io-redirection
我正在交互式shell中执行bash命令
./somescript.sh
它给出了输出
OS platform is: linux2
killall agent
agent: no process killed
Run Code Online (Sandbox Code Playgroud)
其中第三行来自stderr.
但是当我在子shell中执行时
var=$('./somescript.sh' 2>&1)
agent: no process killed
OS platform is: linux2
killall agent
Run Code Online (Sandbox Code Playgroud)
为什么代理人:现在第一行没有打印过程?如何使它们保持一致以对齐它们?
编辑:但是当我这样做时,
var=$('./somescript.sh' 1>&2)
我可以看到它在bash调试模式下以正确的顺序给出输出.但它没有存储在变量var中.
为什么代理人:现在第一行没有打印过程?
我想是的,因为stdout是缓冲的,而stderr不是(或者不是那么多).该行之后这样stderr得到的冲洗agent: no process killed流传输,而stdout剧本后都将刷新./somescript.sh存在.所以第一个出现在屏幕上的是第一个刷新的流 - 即.标准错误.在控制台中运行时,stderr和stdout都设置为行缓冲,int命令替换我猜bash set的stdout要完全缓冲.
如何使它们保持一致以对齐它们?
您可以尝试在命令替换中设置行缓冲. var=$(stdbuf -oL -eL ./somescript.sh 2>&1)