When i ran head file.txt && nl file.txt it did each command in order of occurance (which makes sense). Is it possible to have the head display with numbered lines, so that this:


would become this:

1 word
2 word
3 word
head file.txt | nl

The | creates a pipeline that takes the output of head file.txt and gives it to nl as its "standard" input. Bare nl without a file name will read its standard input and number it, so you get the output of head numbered as you wanted.

Without a pipe providing input, just


would read input from the terminal that you typed. The pipe is a way of providing that data as though you'd typed it in like that.

You can pipe from any command that prints its output, and pipe to any command that reads from the terminal like that, and even pipe several things together:

head -n 50 file.txt | nl | tail -n 20

will give you numbered lines 31-50 from the file.

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.