Bash重定向内部命令

Bash重定向内部命令,bash,Bash,为什么Bash为这两个命令生成两个不同的输出: $ echo $(tput cols 2>/dev/null) 80 $ echo $(tput cols) 141 PS.加宽您的终端,使其具有超过80列(大多数外壳默认为80列)。这似乎是因为stdout和stderr都已被重定向,因此tput不知道您要将信息用于哪个终端 $ tput cols >out; cat out # works because stderr is still the terminal 118

为什么Bash为这两个命令生成两个不同的输出:

$ echo $(tput cols 2>/dev/null)
80
$ echo $(tput cols)
141

PS.加宽您的终端,使其具有超过80列(大多数外壳默认为80列)。

这似乎是因为stdout和stderr都已被重定向,因此
tput
不知道您要将信息用于哪个终端

$ tput cols >out; cat out       # works because stderr is still the terminal
118
$ tput cols 2>err               # works because stdout is still the terminal
118
$ tput cols >out 2>err; cat out # lost track of the terminal, going with default
80

请注意,在您的示例中,stdout被
$()
隐式重定向。解决此问题的一种方法是

$ max_width=`stty -a | sed -n '/columns/s/.*columns \([^;]*\);.*/\1/p' 2>/dev/null`

stty是做你想做的更好的方式

我无法复制此内容,您必须提供更多信息。也无法复制:echo$(tput cols)->80您需要将终端扩宽到超过80列,并且可以复制。我更新了问题以反映这一点。这是“重定向”而不是“管道”。谢谢。不知道它被隐式重定向。可能这是新的:
stty size 2>/dev/null | cut-d”“-f2