在命令替换中将标准错误重定向到 /dev/null时,tput命令会失败
在命令替换中运行 tput cols,同时将标准错误重定向,总会让它回退到80。
除非我把它重定向到一个终端设备文件
tput cols # Output: actual terminal width
tput cols 2>/dev/null # Output: actual terminal width
echo $(tput cols) # Output: actual terminal width
echo $(tput cols 2>/dev/null) # Output: 80
echo $(tput cols 2>/tmp/file.tmp) # Output: 80
var=$(tput cols 2>/dev/null); echo $var # Output: 80
echo $(tput cols 2>/dev/ttys002) # Output: actual terminal width
macOS上的Bash 3.2
为什么?
解决方案
It's because tput reads the data from the terminal device it's on, and if there's no terminal, then it will simply fallback to generic safe data.
只需使用 $COLUMNS,或将 tput 的输出存储到一个变量中
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。