LINES 和 COLUMNS 环境变量在脚本中丢失 [英] LINES and COLUMNS environmental variables lost in a script
问题描述
考虑以下事项:
me@mine:~$ cat a.sh
#!/bin/bash
echo "Lines: " $LINES
echo "Columns: " $COLUMNS
me@mine:~$ ./a.sh
Lines:
Columns:
me@mine:~$ echo "Lines: " $LINES
Lines: 52
me@mine:~$ echo "Columns: " $COLUMNS
Columns: 157
me@mine:~$
变量$LINES
和$COLUMNS
是shell 变量,不是 环境变量,因此不会导出到子进程(但是当我调整 xterm 窗口的大小时,它们会自动更新,即使从远程位置通过 ssh 登录).有没有办法让我的脚本知道当前的终端大小?
The variables $LINES
and $COLUMNS
are shell variables, not environmental variables, and thus are not exported to the child process (but they are automatically updated when I resize the xterm window, even when logged in via ssh from a remote location). Is there a way in which I can let my script know the current terminal size?
我需要这个作为解决方法来解决这个问题:vi(以及 vim、less 和类似命令)每次使用时都会弄乱屏幕.更改终端不是一种选择,因此我正在寻找解决方法(向下滚动 $LINES
行肯定不是完美的解决方案,但至少比丢失前一个屏幕要好)
I need this as a workaround do this problem: vi (as well as vim, less, and similar commands) messes up the screen every time I use it. Changing the terminal is not an option, and thus I'm looking for workarounds (scrolling down $LINES
lines surely is not the perfect solution, but at least is better than losing the previous screen)
推荐答案
您可以从 tput
获取行和列:
You could get the lines and columns from tput
:
#!/bin/bash
lines=$(tput lines)
columns=$(tput cols)
echo "Lines: " $lines
echo "Columns: " $columns
这篇关于LINES 和 COLUMNS 环境变量在脚本中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!