jal*_*anb 16 bash terminal gnome-terminal ansi-term iterm
我正在编写一个脚本,当我进入目录时,它会显示目录的 git 日志。这样的日志可能会让人不知所措,包含数百行。到目前为止,我一直将其限制为硬编码的 20 行 ( ... | head -n 20),这在工作时的屏幕上很好,但在家里较小的 MacBook 屏幕上就太多了。
我希望日志在任一终端上占据大约一半(垂直)屏幕。并且“终端”也发生了变化:它在工作中是 Gnome 终端,但在家里是 iTerm2。而且我不使用屏幕或 tmux。
如何从命令行找到终端中可用的垂直线数?
Cos*_*tas 26
终端参数存储为$LINES和$COLUMNS变量。
此外,您可以使用特殊的术语操作程序,例如tput:
tput lines # outputs the number of lines of the present terminal window.
tput cols # outputs the number of columns of the present terminal window.
Run Code Online (Sandbox Code Playgroud)
Cel*_*ada 10
此命令应为您提供终端上的行数:
stty size | cut '-d ' -f1
Run Code Online (Sandbox Code Playgroud)
有些系统可能没有实现,stty size所以你可能需要这样的东西:
stty -a | tr \; \\012 | grep rows | tr -d ' rows'
Run Code Online (Sandbox Code Playgroud)