Bash subshell & piping to a loop

When setting/changing variables within the scope of a subshell. And then attempt to use those variables outside the scope of this subshell won’t give the ‘expected result’.
A subshell has to be seen as a totally seperate process with it’s own environment.
(check a previous entry about processes)

Now the above might seem logical: Subshell, seperate process, … okay. Let’s say you want to pipe the output of a certain command thru a loop. In this loop you can manipulate/filter/… the data. In some cases you might want to set a flag if a certain criteria is met. Here is where it all comes into play. When you pipe an output to any kind of loop, it’ll become a subshell.

So as you might have guessed. The flag you wanted to set won’t cross it’s subshell borders. So you won’t be able to see it outside the loop. And yet again, an output that you might not have expected.

More info about “bash gotchas” can be found here

6 thoughts on “Bash subshell & piping to a loop

  1. Anything to the right of a pipe is run in a subshell, not just loops. Consider:

    # note, those are curly braces, not parenthesis
    $ echo "blah" | { read foo; }
    $ echo $foo

    Pipes connect two processes, so, each operand must be in a separate process.

    This is a handy, informative site you have, and, if you’ll accept the praise of someone who knows little about Web design, it is clear to read and use. As a professor of CS and, more generally, an educator, as well as someone who’s watched the language degenerate into something less than recognisable, I’d ask that you please mind your grammar:

    > When setting/changing variables within the scope of a subshell.
    > And then attempt to use those variables outside the scope of this
    > subshell won’t give the ‘expected result’.

    Neither of these things is a sentence. And you needn’t quote “expected result”.

    > A subshell has to be seen as a totally seperate process with
    > it’s own environment.

    That’s “…its own environment…”; “it’s” is the contraction, and does not imply possession.

    You are clearly educated. I don’t mean to criticise. I’d just like to see society become a little more proud of its ability to communicate, and it has to start with those who have influence.

    Thank you

  2. Okay, I need to apologise, English is probably not your first language. I just looked at the domain. I should be more observant. But, you speak it well; sadly, better than many Americans. So, please understand my error, and accept my apology.

    Iconoclastes

  3. English isn’t a problem… 😉 The quotes were used to emphasise that part of the sentence.

    Yet thanks for the update on the “Anything to the right of a pipe is run in a subshell, not just loops”-part. I was unaware of this and hopefully this will help people who read this.

  4. I used to write those data into temporary file (using tempfile(1) or mktemp(1)) and then I read it back into the “global” variables.

    Do you know any better/simpler workaround?

  5. Sleep that knits in the ravelled sleave of care The actual death of every day’s existence, sore labour’s bath Balm associated with harm thoughts, great nature’s second course, Chief nourisher in life’s feast.

  6. Even being late, it might be useful for someone stumpling about this post:

    In bash there is the “> echo “blah” | { read foo; }
    could be rewritten as
    >> read foo < > while read line
    >> do something
    >> done < <(command)

    Note however, that the command within the <()-Operator is still executed in a separate subshell.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.