2

This bash snippet will print out the numbers 1 to 10:

$ for i in $(seq 1 10); do echo $i; done
1
2
3
4
5
6
7
8
9
10

When I try to use this with script and less like so:

$ script -q -c 'for i in $(seq 1 10); do echo $i; done' /dev/null | less

...I see this instead:

1
 2
  3
   4
    5
     6
      7
       8
        9
         10
           (END)

The usual less prompt doesn't show up, either. What's going on here?

I'm doing this on Ubuntu 13.10, script is script from util-linux 2.20.1, less is less 458 (GNU regular expressions).

1 Answer 1

4

You're seeing output to the terminal while it is not in NLCR mode (stty -onlcr).

Normally the kernel driver for ttys outputs a carriage return along with a newline whenever a newline is output. With NLCR mode disabled newlines are output as is, which moves the cursor down one line but leaves it in the same column. This produces the staggered output you see above. The reason the terminal is in this mode is less sets it that way in preparation for doing its job, which is paginating output.

The "(END)" that you see is less announcing the end of input condition. Since script is writing to /dev/null and to the terminal and not to stdout, less never receives anything on stdin. Since script is reading from the terminal, less receives EOF when it tries to also read a command from the terminal and quits.

3
  • Is there a way to replicate more "traditional" terminal/less interaction? I need to use script to get color output out of the executable I'm running, but the output is unreadable with the line endings the way they are.
    – detly
    Dec 5, 2013 at 2:04
  • Can you be more specific about why you need to use script to get color output? Is it because the executable you're running changes its output depending on where the output is going? If so, could you not just send the output of the executable to, say, a file, and then process that file in a separate step?
    – dg99
    Dec 6, 2013 at 16:31
  • I cannot for the life of me remember what I was doing when I asked this question, but it's a good explanation.
    – detly
    Sep 9, 2016 at 12:58

You must log in to answer this question.

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