7

Summary: w | tail chops rows after 78th column.

When I run the "w" command, it works as expected. However, when I pipe the output to "tail", it chops the output to 78 columns wide (truncates anything after right margin).

I've checked set and env for any values of 78 (or 79 or 80), as well as TERM (xterm|ansi|vt100), and stty -a settings, as well as tail and w (they look OK). $COLUMNS is set to 209 (but this doesn't matter...I set it to 100 with the same results).

I'm running CentOS 5 (kernel - 2.6.18). The tail and w binaries look OK to me.

/usr/bin/w: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped

CentOS$ cat 80.txt
          1         2         3         4         5         6         7         8
012345678901234567890123456789012345678901234567890123456789012345678901234567890
----------+---------+---------+---------+---------+---------+---------+---------+
CentOS$ export COLUMNS=100
CentOS$ w | tail -5 | sed 's/./\./g'
.....................................................................
...............................................................................
...............................................................................
...............................................................................
...............................................................................
  • Try alias w='w | more. That helped me alot. – ott-- May 13 '13 at 20:28
8

When w's output goes to a terminal. w queries the terminal driver for the number of columns and adjusts the width accordingly.

When output doesn't go to a terminal such as when it goes to a pipe, it reverts to a fixed width of 80 columns. Versions of procps since 3.3.2 allow to override that default with the $COLUMNS environment variable.

Some shells like zsh or bash set the $COLUMNS variable, but don't export it. In them, you can do

COLUMNS=$COLUMNS w | tail

If you don't want the lines to be truncated at all regardless of how wide the terminal is, you can do:

COLUMNS=512 w | tail

If you've got an older w, you probably won't have any option but to emulate a wide terminal. With zsh:

zmodload zsh/zpty
zpty W 'stty cols 512 -opost && w'
{zpty -r W} | tail
zpty -d W

Other things that can emulate a terminal:

ssh localhost -t 'stty cols 512 -opost && w' | tail

expect -c 'set stty_init {cols 512 -opost}; spawn -noecho w; expect eof' | tail
  • I'm using bash 4.2.0(1), w version procps 3.2.7 in a CentOS 5.3. The COLUMNS var doesn't make any different – RSFalcon7 May 13 '13 at 14:52
  • If I export the COLUMNS variable, shouldn't w use that setting? [tried this] – Razzlephrazz May 13 '13 at 14:53
  • COLUMNS=0 doesn't work for me. However, COLUMNS=1000 does. – peterph May 13 '13 at 15:01
  • @Razzlephrazz, yes, though that would affect other commands as well. Your problem is probably that you have too old a version of w to support overriding the width. – Stéphane Chazelas May 13 '13 at 15:06
  • Thanks, Stephane. Do you know how I can find out how old is my "w" and when is the version where it was fixed created? This apparently is a bug in the "w" command. Or perhaps my version of the w command uses another ENV var rather than COLUMNS. – Razzlephrazz May 13 '13 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.