2

我在 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

我的环境:

  • Mac mini 2018 年末
  • macOS 11.4
  • 'uname -a' 的结果是 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
  • $BASH_VERSION 是 3.2.57(1)-release
4

1 回答 1

1

当标准输出连接到终端时,tput将查询终端设备以获取窗口尺寸。

当您tput输入命令替换时$(tput lines),它的标准输出连接到管道,而不是终端。在这种情况下,它将使用LINES环境变量,或terminfo终端类型条目中的默认大小。

由于您LINES=50在使用管道时有但没有得到它,我怀疑您忘记导出变量。采用

export LINES=50

然后再试一次。

更新:

我误读了这个例子(很难说出每个输出都有哪些命令)。现在我认为当标准输出不是终端时,它会尝试标准错误。只有当它们都不是终端时,它才会回退到默认值。所以24当标准输出是命令替换的管道,标准错误被重定向到/dev/null.

于 2021-07-23T19:17:22.553 回答