【发布时间】:2022-01-19 06:29:42
【问题描述】:
在 Linux bash 中,我看到使用 |& 运算符的脚本,例如:
/opt/program.sh |& tee -a /var/log/program.log
|&运算符是什么意思?
问候
【问题讨论】:
在 Linux bash 中,我看到使用 |& 运算符的脚本,例如:
/opt/program.sh |& tee -a /var/log/program.log
|&运算符是什么意思?
问候
【问题讨论】:
根据§3.2.3 "Pipelines" in the Bash Reference Manual,|& 类似于|,除了:
如果使用'
|&',command1的标准错误,除了它的标准输出外,还通过管道连接到command2的标准输入;它是2>&1 |的简写。标准错误到标准输出的这种隐式重定向是在命令指定的任何重定向之后执行的。
也就是说——/opt/program.sh 打印到标准输出 或 到标准错误的任何内容都将通过管道传输到 tee -a /var/log/program.log。
【讨论】:
除了ruakh's answer之外,这里有一些尝试,作为In the shell, what does " 2>&1 " mean?的补全
ls -ld /t{mp,nt} | wc
ls: cannot access '/tnt': No such file or directory
1 9 49
ls -ld /t{mp,nt} 2>&1 | wc
2 18 101
ls -ld /t{mp,nt} |& wc
2 18 101
但由于命令行顺序很重要,这将被视为最后一次重定向:
ls -ld /t{mp,nt} 2>&1 1>/dev/tty | wc
drwxrwxrwt 11 root root 4096 Jan 19 08:39 /tmp
1 9 52
ls -ld /t{mp,nt} 1>/dev/tty |& wc
0 0 0
ls: cannot access '/tnt': No such file or directory
drwxrwxrwt 11 root root 4096 Jan 19 08:39 /tmp
【讨论】: