[英]What changes the result of the 'tput lines' command?
我在 LINES=50 的终端上的 bash 上执行了以下代码。 'tput lines' 命令的结果各不相同。 我不知道解释这个结果。 为什么 'tput lines' 命令可能返回 '24' ?
代码:
#!/bin/sh
echo $LINES
tput lines
tput lines 2>/dev/null
echo $(tput lines)
echo $(tput lines 2>/dev/null)
true && tput lines
true && tput lines 2>/dev/null
true && echo $(tput lines)
true && echo $(tput lines 2>/dev/null)
echo $TERM
echo $TERM 2>/dev/null
echo $(echo $TERM)
echo $(echo $TERM 2>/dev/null)
true && echo $TERM
true && echo $TERM 2>/dev/null
true && echo $(echo $TERM)
true && echo $(echo $TERM 2>/dev/null)
结果1:
$ ./testtput
50
50
50
24
50
50
50
24
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
结果2:
$ ./testtput | pbcopy
50
24
50
24
50
24
50
24
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
我的环境:
Darwin hogehoge.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
Darwin hogehoge.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
3.2.57(1)-release
当标准输出连接到终端时, tput
将查询终端设备以查找获取窗口尺寸。
当您将tput
放入命令替换$(tput lines)
,其标准输出连接到管道,而不是终端。 在这种情况下,它将使用LINES
环境变量,或终端类型的terminfo
条目中的默认大小。
由于您有LINES=50
但在使用管道时没有得到,我怀疑您忘记导出变量。 采用
export LINES=50
然后再试一次。
更新:
我误读了这个例子(很难判断每个输出对应哪些命令)。 现在我认为当标准输出不是终端时,它会尝试标准错误。 只有当它们都不是终端时才回退到默认值。 因此,当标准输出是命令替换的管道时,您会得到24
,并且标准错误被重定向到/dev/null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.