Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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).

share|improve this question
add comment

1 Answer 1

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.

share|improve this answer
    
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 '13 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 '13 at 16:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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