Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to view the last few lines of a file, but I want the columns aligned. Basically, I want to disable wordwrap.

This is straightforward with:

tail $FILE | cut -c -80

But I'm trying to generalize my script for other users, and I'd like to cut to the actual terminal width, which I can get with:

stty size | cut -d" " -f2

So I would imagine that I could just

tail $FILE | cut -c -`stty size | cut -d" " -f2`

but it doesn't work:

stty: standard input: Invalid argument
cut: invalid range with no endpoint: -
Try `cut --help' for more information.

(Same results with the 'new' $() expansion.)

Now, if I echo it, it seems fine:

echo cut -c -`stty size | cut -d" " -f2`
cut -c -103

Am I just missing an escape char? Or is this somehow just not possible?

Thanks.

share|improve this question
up vote 3 down vote accepted

The reason that it doesn't work is because stty is executed within a pipe. Therefore it doesn't "see" the underlying terminal. In your script you could store the terminal width in a variable like

size=`stty size | cut -d" " -f2`

and then use that next:

tail $FILE | cut -c -$size
share|improve this answer
    
Thanks! I knew is was something simple! – Joe Fruchey Jun 24 '13 at 14:13

Bash maintains the screen width in the COLUMNS variable, which you can use in a pipeline:

tail $FILE | cut -c -$COLUMNS
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .