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 8948791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:05:07+00:00 2026-06-15T13:05:07+00:00

I’ve been trying to read input into environment variables from program output like this:

  • 0

I’ve been trying to read input into environment variables from program output like this:

echo first second | read A B ; echo $A-$B 

And the result is:

-

Both A and B are always empty. I read about bash executing piped commands in sub-shell and that basically preventing one from piping input to read. However, the following:

echo first second | while read A B ; do echo $A-$B ; done

Seems to work, the result is:

first-second

Can someone please explain what is the logic here? Is it that the commands inside the while … done construct are actually executed in the same shell as echo and not in a sub-shell?

  • 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-15T13:05:09+00:00Added an answer on June 15, 2026 at 1:05 pm

    How to do a loop against stdin and get result stored in a variable

    Under bash (and other shell also), when you pipe something to another command via |, you will implicitly create a fork, a subshell that is a child of current session. The subshell can’t affect current session’s environment.

    So this:

    TOTAL=0
    printf "%s %s\n" 9 4 3 1 77 2 25 12 226 664 |
      while read A B;do
          ((TOTAL+=A-B))
          printf "%3d - %3d = %4d -> TOTAL= %4d\n" $A $B $[A-B] $TOTAL
        done
    echo final total: $TOTAL
    

    won’t give expected result! :

      9 -   4 =    5 -> TOTAL=    5
      3 -   1 =    2 -> TOTAL=    7
     77 -   2 =   75 -> TOTAL=   82
     25 -  12 =   13 -> TOTAL=   95
    226 - 664 = -438 -> TOTAL= -343
    echo final total: $TOTAL
    final total: 0
    

    Where computed TOTAL could’nt be reused in main script.

    Inverting the fork

    By using bash Process Substitution, Here Documents or Here Strings, you could inverse the fork:

    Here strings

    read A B <<<"first second"
    echo $A
    first
    
    echo $B
    second
    

    Here Documents

    while read A B;do
        echo $A-$B
        C=$A-$B
      done << eodoc
    first second
    third fourth
    eodoc
    first-second
    third-fourth
    

    outside of the loop:

    echo : $C
    : third-fourth
    

    Here Commands

    TOTAL=0
    while read A B;do
        ((TOTAL+=A-B))
        printf "%3d - %3d = %4d -> TOTAL= %4d\n" $A $B $[A-B] $TOTAL
      done < <(
        printf "%s %s\n" 9 4 3 1 77 2 25 12 226 664
    )
      9 -   4 =    5 -> TOTAL=    5
      3 -   1 =    2 -> TOTAL=    7
     77 -   2 =   75 -> TOTAL=   82
     25 -  12 =   13 -> TOTAL=   95
    226 - 664 = -438 -> TOTAL= -343
    
    # and finally out of loop:
    echo $TOTAL
    -343
    

    Now you could use $TOTAL in your main script.

    Piping to a command list

    But for working only against stdin, you may create a kind of script into the fork:

    printf "%s %s\n" 9 4 3 1 77 2 25 12 226 664 | {
        TOTAL=0
        while read A B;do
            ((TOTAL+=A-B))
            printf "%3d - %3d = %4d -> TOTAL= %4d\n" $A $B $[A-B] $TOTAL
        done
        echo "Out of the loop total:" $TOTAL
      }
    

    Will give:

      9 -   4 =    5 -> TOTAL=    5
      3 -   1 =    2 -> TOTAL=    7
     77 -   2 =   75 -> TOTAL=   82
     25 -  12 =   13 -> TOTAL=   95
    226 - 664 = -438 -> TOTAL= -343
    Out of the loop total: -343
    

    Note: $TOTAL could not be used in main script (after last right curly bracket } ).

    Using lastpipe bash option

    As @CharlesDuffy correctly pointed out, there is a bash option used to change this behaviour. But for this, we have to first disable job control:

    shopt -s lastpipe           # Set *lastpipe* option
    set +m                      # Disabling job control
    TOTAL=0
    printf "%s %s\n" 9 4 3 1 77 2 25 12 226 664 |
      while read A B;do
          ((TOTAL+=A-B))
          printf "%3d - %3d = %4d -> TOTAL= %4d\n" $A $B $[A-B] $TOTAL
        done
    
      9 -   4 =    5 -> TOTAL= -338
      3 -   1 =    2 -> TOTAL= -336
     77 -   2 =   75 -> TOTAL= -261
     25 -  12 =   13 -> TOTAL= -248
    226 - 664 = -438 -> TOTAL= -686
    
    echo final total: $TOTAL
    -343
    

    This will work, but I (personally) don’t like this because this is not standard and won’t help to make script readable. Also disabling job control seem expensive for accessing this behaviour.

    Note: Job control is enabled by default only in interactive sessions. So set +m is not required in normal scripts.

    So forgotten set +m in a script would create different behaviours if run in a console or if run in a script. This will not going to make this easy to understand or to debug…

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.