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

  • SEARCH
  • Home
  • 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 8557079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:29:38+00:00 2026-06-11T15:29:38+00:00

A fun little bash teaser to which I’d love an explanation. Two loop constructs

  • 0

A fun little bash teaser to which I’d love an explanation.

Two loop constructs I would have though are identical, clearly are not. Seems there’s some difference in piping vs redirecting when doing a while loop.

Input File

Given this sample file called values.txt with this content:

1
2
3
4
5
6

Piping to while

$ value=0; cat values.txt | while read var; do value=`expr $value + $var`; done
$ echo $value
0

Redirecting to while

$ value=0; while read var; do value=`expr $value + $var`; done < values.txt
$ echo $value
21

To be brief, clearly in the first version each iteration of the while loop executes effectively as () and in the second each iteration iterates as {}

The question is not the difference between () and {}. My question is what causes this difference in behavior for while loops?

Is there a logical reason they should behave differently or was it just a bad choice made early on that couldn’t be changed for compatibility reasons? Is it ever possible to pipe to while and get {} behavior?

  • 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-11T15:29:40+00:00Added an answer on June 11, 2026 at 3:29 pm

    This is a known problem and is well explained here:
    http://mywiki.wooledge.org/BashFAQ/024

    To quote the most explanatory part:

    Different shells exhibit different behaviors in this situation:

    • BourneShell creates a subshell when the input or output of anything
      (loops, case etc..) but a simple command is redirected, either by
      using a pipeline or by a redirection operator (‘<‘, ‘>’).
    • BASH creates a new process only if the loop is part of a pipeline.
    • KornShell
      creates it only if the loop is part of a pipeline, but not if the loop
      is the last part of it.
    • POSIX specifies the bash behaviour, but as
      an extension allows any or all of the parts of the pipeline to run
      without a subshell (thus permitting the KornShell behaviour, as well).

    As for the last question: yes, it is possible in certain shells, and in bash only if you have bash >=4.2, and preceed your code with disabling job control and enabling lastpipe option with the following code: set +m; shopt -s lastpipe

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

Sidebar

Related Questions

I'm developing a fun little project using Foursquare API. In this project, I have
I'm programming a little Twitter Client just for fun. I have the tweet's text
I have a fun little problem where I'm presented with a logic clause like
I'm developing a little C# application for the fun. I love this language but
I have a small little program I am working on just for fun. It's
I am developing a quick and fun little app which will display in a
I have an idea for a fun little service that relies on data from
I'm making myself a little messenger just for fun. And I want to make
I'm starting to learn a little Flex just for fun and maybe to prove
While making a little Pong game in C++ OpenGL, I decided it'd be fun

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.