I'm trying to get the width of the current terminal window in a script and use 80 as a fallback in case that isn't possible. I thought that's pretty simple:
cols=$( tput cols || echo 80 ) ; echo $cols
# -> 100
100 is correct, I made the terminal 100 chars wide. As cols
is no POSIX conform argument to tput
, not all systems will support it, thus the fallback. Now let's test the fallback:
cols=$( tput colsx || echo 80 ) ; echo $cols
# -> tput: unknown terminfo capability 'colsx'
# -> 80
Hmmmm... not so nice. I don't want to see that error. It's printed on stderr
, so let's just suppress it:
cols=$( tput colsx 2>/dev/null || echo 80 ) ; echo $cols
# -> 80
Yes, that's much better. So the final code is
cols=$( tput cols 2>/dev/null || echo 80 ) ; echo $cols
# -> 80
80? WTF? Why does it run into the fallback now? Let's try this:
cols=$( tput cols 2>/dev/null ) ; echo $cols
# -> 80
Ahhhh... redirecting stderr
changes the output of tput
? How's that possible? Let's confirm that:
tput cols 2>/dev/null
# -> 100
Okay, now I'm lost! Can someone please explain me what's going on here?