Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:11:18+00:00 2026-06-14T20:11:18+00:00

I need to read from a continuous data stream (pipe, actually), line by line,

  • 0

I need to read from a continuous data stream (pipe, actually), line by line, and I need to exit after the 1st line. RIGHT after the 1st line. Sounded pretty easy but, using “head -n 1”, I noticed that I actually need to enter a second line before head exits.

Test case:

[s@svr1 ~]$ cat | head -n 1    
123    <- I type this first (followed by enter, of course)
123    <- I get this output from head, but the command does no exit
456    <- Then I need to type this for the command to exit and bring me back to the prompt
[s@svr1 ~]$

Can someone explain (first and foremost) why it’s acting like that, and maybe how I could get what I need?
(and I want to stick to basic Linux/Unix lightweight building blocks. No Perl, Python and such…)

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:11:19+00:00Added an answer on June 14, 2026 at 8:11 pm

    Because you’re using cat | head -n 1, which is a useless use of cat and not the same as head -n 1. If you do head -n 1 at the console you get the behavior you want — head reads one line, prints it, and exits.

    If you do cat | head -n 1, then this happens:

    1. cat reads “123” from its input.
    2. cat writes “123” to its output.
    3. head reads “123” from its input (which is connected to cat‘s output).
    4. head writes “123” to its output and exits.
    5. cat reads “456” from its input.
    6. cat tries to write “456” to its output.
    7. cat gets SIGPIPE because the process on the other side of its output has died.
    8. cat exits.

    cat begins another read as soon as it’s written “123” to head, and it doesn’t find out that head has died until it tries to write a second line to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read data from XML to a List<>. The XML file contains
I need to read data from a legacy database file produced by Visual Basic
Actually in one of my project i need to read images from remote server
As part of an assignment, I need to read data from a binary file
Hi there I'm use to SQL, but I need to read data from a
I need to read from a file and then break the line into 3
I need to read from the same stream twice. The problem is i get
I need to read from a Stream and put the buffer that was read
I'm looking for the best way to read data from an stdin pipe in
I need to read from and write to Excel spreadsheets using Delphi 2010. Nothing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.