我在带有LINES=50的终端bash上执行了以下代码。“tput line”命令的结果各不相同。我不知道如何解释这个结果。为什么“tput line”命令可能返回“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我的环境:
2018年末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
发布于 2021-07-23 19:17:22
当标准输出连接到终端时,tput将查询终端设备以获取窗口尺寸。
当您将tput放入命令替换( $(tput lines) )中时,它的标准输出将连接到管道,而不是终端。在这种情况下,它将使用LINES环境变量,或者终端类型的terminfo条目的默认大小。
因为您有LINES=50,但在使用管道时没有得到它,所以我怀疑您忘记了导出变量。使用
export LINES=50然后再试一次
更新:
我误读了这个示例(很难判断每个输出都有哪些命令)。现在我认为,当标准输出不是终端时,它会尝试标准错误。只有当它们都不是终端时,它才会回到默认状态。因此,当标准输出是命令替换管道,而标准错误被重定向到/dev/null时,您就得到了/dev/null。
https://stackoverflow.com/questions/68503731
复制相似问题