word wrap in ssh session not working with grep

user53029 asked:

When I ssh to my Linux servers and use grep like this:

grep ‘timeout exceeded’ logfile | less

word wrap does not work.

However, if I use the same command but use less first, like this:

less logfile | grep ‘timeout exceeded’

the lines wrap. I am not sure what the problem is or if this is normal or not. But it happens regardless of the ssh client I use. I have tried both putty and an Ubuntu client. How can I fix this?

My answer:

This is not the default behavior of less. The default is to wrap long lines.

You are seeing this behavior because you have the -S option (and several others) set in your LESS environment variable.

       -S or --chop-long-lines
              Causes  lines  longer than the screen width to be chopped (trun‐
              cated) rather than wrapped.  That is, the portion of a long line
              that does not fit in the screen width is not shown.  The default
              is to wrap long lines; that is, display  the  remainder  on  the
              next line.

To resolve the problem, check your shell startup scripts (e.g. $HOME/.bash_profile, $HOME/.bashrc) and the system shell startup scripts (e.g. those in the /etc/profile.d directory) to see where the environment variable is being set, and make the desired changes.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.