What is the significance of importance of the command export COLUMNS
?
What I know is that it's a global variable.
I frequently see it in the beginning of *nix scripts.
It is the width of your current terminal window measured as the number of ASCII characters.
From man bash
:
COLUMNS
Used by the select builtin command to determine the terminal
width when printing selection lists. Automatically set upon
receipt of a SIGWINCH.
Also in the more generic ksh
:
COLUMNS
If this variable is set, the value is used to define the
width of the edit window for the shell edit modes and for
printing select lists.
For practical part, notice that the value of this variable changes when the dimensions of your virtual terminal window change. (This is when the above mentioned SIGWINCH
is sent to the active shell).
This answer is something of a guess, given that you didn't say on what kind of system you see this, Solaris, HP-UX, AIX, a BSD, Linux (what distro?), etc...
Shell scripts that did curses-style interaction with users would set environment variables ROWS and COLUMNS in the past.
Curses-style interactivity dates from before windowing systems, so those programs typically didn't do whatever it took to understand what size an xterm window was. So a lot of curses-based programs used environment variables ROWS and COLUMNS to decide how to lay out their fields and labels.
Sometimes, shells would try to find and set ROWS and COLUMNS environment variables when they started running in an xterm. A lot of older SunOS and Solaris systems would have /etc/profile do this. Sometimes a SIGWINCH (in SunOS and Solaris, at least) would get used to set the ROWS and COLUMNS variables. Vendors used to modify xterm in silly ways, and often would ruin passing SIGWINCH to the process group running in an xterm, and curses-style interaction would look really bad.
Literally, it tells certain applications the number of character positions that the terminal width corresponds to. Almost all terminals use a regular grid of lines and columns.
LINES
and COLUMNS
were originally (around 1980) internal variables in vi
and curses
. Later, shell variables with those names were introduced. While they may have been supported in SunOS4, the only mention in the manual pages is for curses and ls
:
In order to determine output formats for the
-C
,-x
, and-m
options,/usr/5bin/ls
uses an environment variable,COLUMNS
, to determine the number of character positions available on one output line. If this variable is not set, the terminfo database is used to determine the number of columns, based on the environment variableTERM
. If this information cannot be obtained, 80 columns are assumed.
That is, the feature came from System V, and appears to have been introduced later, e.g., this commit in 1989, for the BSD version of ls
.
SVr4 curses (and its successor X/Open Curses) documents the use of environment variables LINES
and COLUMNS
which can be used to override the terminal description's values for the terminal size, subject to the use_env
function. The ncurses manual page documents this feature.
Likewise, its introduction to shell programming appears to have come from System V, in ksh
. When and how SIGWINCH
was combined with these variables, is elusive, probably debatable. However, in current shells at hand these set shell variables (not environment variables) in response to SIGWINCH
: ksh93
, bash
. It seems that zsh
sets it without SIGWINCH
(likewise fish
). Checking Solaris 10, ksh88
did not set the variables.
The older changelog in bash 1.13 (released September 1993) said the SIGWINCH
feature was added in March 1993, while Sven Mascheck's page implies that ksh93 was released around the end of the same year.
Having the value in a shell variable makes it easy to export (and change it to an environment variable), and modify the behavior of certain programs. But the reason for the variable came from its usefulness when terminal sizes were hard to determine.