Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2008-05-24 07:52:28
Size: 496
Editor: pgas
Comment: I'm trying to get the number of columns/lines but the variables COLUMNS / LINES are always empty
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 1: Line 1:
[[Anchor(faq91)]] <<Anchor(faq91)>>
Line 3: Line 3:
`COLUMNS` and `LINES` are set by [[BASH]] only in interactive shells; they do not work in a script. Instead you can use:
Line 4: Line 5:
COLUMNS and LINES are set by bash only in interactive shells, they do not work in a script.
Instead you can use:
Line 11: Line 10:
If you want these variables to but updated when the terminal is resized, ie upon receipt of a SIGWINCH, you
can set the trap yourself:
`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:
Line 14: Line 14:
trap 'COLUMNS=$(tput cols)' WINCH trap 'COLUMNS=$(tput cols) LINES=$(tput lines)' WINCH
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)