So I'm working on a script and found some odd behaviour. I'm sure there is a logical explanation why the output of the 4th and 6th command line is different than in the other cases, but I fail to find it.
1 $ tput cols
128
2 $ tput cols 2>/dev/null
128
3 $ echo $(tput cols)
128
4 $ echo $(tput cols 2>/dev/null)
80
5 $ (tput cols >/tmp/cols.txt); cat /tmp/cols.txt
128
6 $ (tput cols &>/tmp/cols.txt); cat /tmp/cols.txt
80
7 $ echo $(tput cols 2>/dev/null; echo $COLUMNS; tput cols)
80 128 128
Why does stderr redirection change the output of tput in a subshell?
Ultimately I want to do something like this in my script to make it work on systems where tput/ncurses isn't available:
cols=$(tput cols 2>/dev/null || echo $COLUMNS)
The example above was created with Bash 4.3.46(1)-release and ncurses 6.0.20150627