LINES和COLUMNS环境变量在脚本中丢失 [英] LINES and COLUMNS environmental variables lost in a script

查看:479
本文介绍了LINES和COLUMNS环境变量在脚本中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

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?

编辑:
我需要这个作为一种解决方法做一个 $ 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

#!/bin/bash

lines=$(tput lines)
columns=$(tput cols)

echo "Lines: " $lines
echo "Columns: " $columns

这篇关于LINES和COLUMNS环境变量在脚本中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆