Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINES and COLUMNS environmental variables lost in a script

Consider the following:

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:~$  

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?

EDIT: 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)

like image 639
Davide Avatar asked Nov 23 '09 00:11

Davide


People also ask

What are environmental variables in scripting?

Environment variables are special variables (like $HOME ) that contain information about your login session. They're stored for the system shell to use when executing commands. They exist whether you're using Linux, Mac, or Windows. Many of these variables are set by default during installation or user creation.

How do I get a list of environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.

How do you display environment variables?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.

What is the command to list the environment variables and their values?

The printenv command displays the values of environment variables. If the name argument is specified, only the value associated with name is printed.


1 Answers

You could get the lines and columns from tput:

#!/bin/bash  lines=$(tput lines) columns=$(tput cols)  echo "Lines: " $lines echo "Columns: " $columns 
like image 106
Puppe Avatar answered Oct 02 '22 10:10

Puppe