Differences between revisions 4 and 6 (spanning 2 versions)
Revision 4 as of 2008-11-22 23:33:58
Size: 995
Editor: GreyCat
Comment: first-line
Revision 6 as of 2012-08-19 06:09:59
Size: 1829
Editor: ormaaj
Comment:
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
`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`).
Line 16: Line 14:

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

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

Though not the best practice, it's not too uncommon for scripts to test for the `-i` option to determine whether a shell is interactive. There is no completely foolproof way to test for this, so some scripts may break as a result. Also, running with -i sources `.bashrc`, and sets various options such as job-control which may have unintended side-effects. Though you can technically set `-i` in the middle of a script, it has no effect on the setting of `COLUMNS` and `LINES` -- `-i` must be set when Bash is first invoked.

If `COLUMNS` / `LINES` aren't being updated even when running an interactive shell, ensure that the `checkwinsize` shopt option is enabled.

`tput`, of course, requires a terminal. According to POSIX, if stdout is not a tty, then results are unspecified, and that stdin is unused, though some implementations may try using it anyway. On this Gentoo Linux system (and apparently at least some other Linuxes), at least one of stdout or stderr must be a tty, otherwise tput just returns some default value (here it's "80").

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)

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

Though not the best practice, it's not too uncommon for scripts to test for the -i option to determine whether a shell is interactive. There is no completely foolproof way to test for this, so some scripts may break as a result. Also, running with -i sources .bashrc, and sets various options such as job-control which may have unintended side-effects. Though you can technically set -i in the middle of a script, it has no effect on the setting of COLUMNS and LINES -- -i must be set when Bash is first invoked.

If COLUMNS / LINES aren't being updated even when running an interactive shell, ensure that the checkwinsize shopt option is enabled.

tput, of course, requires a terminal. According to POSIX, if stdout is not a tty, then results are unspecified, and that stdin is unused, though some implementations may try using it anyway. On this Gentoo Linux system (and apparently at least some other Linuxes), at least one of stdout or stderr must be a tty, otherwise tput just returns some default value (here it's "80").

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