Curious tput behavior with stderr redirection

I am trying to use tputa Bash script and do my best to avoid a random error. To this end, I wrote the following line:

COLS="$(tput cols 2> /dev/null)"

To my surprise, when I run this, it is COLSsequentially set to 80, no matter what the window width of my terminal. (For demonstration, my terminal has a width of 115 columns.) To find out what was going on, I tried a few things on the command line:

$ tput cols
115
$ tput cols | cat
115
$ echo "$(tput cols)"
115
$ tput cols 2> /dev/null
115
$ echo "$(tput cols 2> /dev/null)"
80

Thus tput, it seems to succeed in defining the characteristics of the terminal when its stderr is redirected or when it is built into the process substitution, but not for both. How strange!

I tested this on both Linux and OS X, and the behavior is the same.

? , tput stderr spew?

. $COLUMNS. tput.

+4
2

strace , tput stdout, , stderr. , , tput (-) 80 .

+5

Google , . , , , . , .

, cron. cron Solaris TERM "", tput , ( cron). stdout, , , tput , tput cols. , , :

declare TPUT C
TPUT="$(tput longname 2>/dev/null)"
[[ "$TPUT" ]] && C="$(tput cols)"
C=${C:-80}

, , , , , tput .

0

Source: https://habr.com/ru/post/1526800/


All Articles