Differences between revisions 4 and 5
Revision 4 as of 2008-11-22 23:33:58
Size: 995
Editor: GreyCat
Comment: first-line
Revision 5 as of 2009-10-30 19:24:26
Size: 1110
Comment: bash -i in a script's shebang
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:

You can also set the shell as interactive in the script's shebang:

{{{
 #!/bin/bash -i
 echo $COLUMNS
}}}

I'm trying to get the number of columns or lines of my terminal but the variables COLUMNS / LINES are always empty

COLUMNS and LINES are set by BASH only in interactive shells; they do not work in a script. Instead you can use:

COLUMNS=$(tput cols)
LINES=$(tput lines)

tput, of course, requires a terminal. On Debian, OpenBSD and HP-UX, tput needs standard output to be a terminal. However, the behavior of tput is non standardized; it is left up to the implementation. It's possible that some operating systems might require standard input to be the terminal device instead (a la stty).

Bash automatically updates the COLUMNS and LINES variables when an interactive shell is resized. If you're setting the variables in a script and you want them to be updated when the terminal is resized, i.e. upon receipt of a SIGWINCH, you can set a trap yourself:

trap 'COLUMNS=$(tput cols) LINES=$(tput lines)' WINCH

You can also set the shell as interactive in the script's shebang:

 #!/bin/bash -i
 echo $COLUMNS

BashFAQ/091 (last edited 2016-10-22 17:32:20 by tor-2)