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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:13:01+00:00 2026-05-29T07:13:01+00:00

In a comment on another post , @JonathanLeffler stated that: { … } |

  • 0

In a comment on another post, @JonathanLeffler stated that:

{ … } | somecommand is run in a sub-shell and doesn’t affect the
parent shell. Demo:

X=PQR; echo $X; { X=ABC; echo $X; } | cat; echo $X

(with output PQR, ABC, PQR on three lines)

and indeed:

james@bodacious-wired:tmp$X=PQR; echo $X; { X=ABC; echo $X; } | cat; echo $X
PQR
ABC
PQR

However, man bash says that { .. } does not execute in a subshell:

   { list; }
          list  is  simply executed in the current shell environment.  list must be 
          terminated with a newline or semicolon.  This is known as a group command. 

So what’s going on here? Is man bash wrong? I know that each part of a pipeline executes in a subshell; but I don’t see how that causes the observed behaviour. For instance:

james@bodacious-wired:tmp$X=PQR; echo $X | sed;  X=ABC; echo $X | sed; echo $X
PQR
ABC
ABC

Edited to add:

A few people have suggested using echo $$ to show that things are (or are not) parts of a subshell. This is not at all useful, as the $$ gets expanded during the parameter expansion stage, which happens long before any commands are executed.

As an example:

james@bodacious-wired:tmp$echo 1$$; ps; ( echo 2$$; ps ); echo 3$$; ps
11194
  PID TTY           TIME CMD
 1194 ttys000    0:00.22 -bash
21194
  PID TTY           TIME CMD
 1194 ttys000    0:00.22 -bash
 7894 ttys000    0:00.00 -bash
31194
  PID TTY           TIME CMD
 1194 ttys000    0:00.22 -bash
james@bodacious-wired:tmp$

You can see that the second invocation of ps happened inside a subshell, with pid 7894; but echo 2$$ still shows the value that bash substituted in the variable expansion phase, before it spawned the subshell

For contrast, and demonstrating that { .. } does not spawn a subshell:

james@bodacious-wired:tmp$echo 1$$; ps; { echo 2$$; ps; }; echo 3$$; ps
11194
  PID TTY           TIME CMD
 1194 ttys000    0:00.22 -bash
21194
  PID TTY           TIME CMD
 1194 ttys000    0:00.22 -bash
31194
  PID TTY           TIME CMD
 1194 ttys000    0:00.23 -bash

Just to prove that @nos is correct, add a pipeline to the above:

james@bodacious-wired:tmp$echo 1$$; ps; { echo 2$$; ps; } | sed ; echo 3$$; ps
11194
  PID TTY           TIME CMD
 1194 ttys000    0:00.25 -bash
21194
  PID TTY           TIME CMD
 1194 ttys000    0:00.25 -bash
 7945 ttys000    0:00.00 -bash
 7946 ttys000    0:00.00 sed
31194
  PID TTY           TIME CMD
 1194 ttys000    0:00.25 -bash

As expected, the shell spawns two subshells, one for each side of the pipeline.

  • 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-05-29T07:13:02+00:00Added an answer on May 29, 2026 at 7:13 am

    Each side of a pipeline becomes a subshell at least.

    X=PQR; echo $X; { X=ABC; echo $X; } | cat; echo $X
    

    will make a subshell/process of , atleast { X=ABC; echo $X; } and cat.

    "Each command in a pipeline is executed as a separate process (i.e., in a subshell)." , from man bash

    If you instead do this

    X=PQR; echo $X; { X=ABC; echo $X; } ; echo $X
    

    You’ll see afterwards that echo $X shows ABC.

    There’s other ways that commands are executed in subshells too, e.g. if you background a subcommand: { X=SUB ; sleep 1; } & , that group will run in a subshell, whereas just { X=SUB ; sleep 1; } will not.

    If you want to group commands that always execute in a subshell, use parenthesis, (X=ABC ; echo $X) instead of braces.

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

Sidebar

Related Questions

In a comment on this answer to another question , someone said that they
<script type=text/javascript> var dataString2 = 'run=captchagood&comment=' + comment; $.ajax({ type: POST, url: process.php, data:
I have nested attributes for say comment. The parent class is post. <% form_for
I've asked this question as a comment on another post but couldn't get it
I have a simple comment section where users can post comments to another users
The comment to this answer got me wondering. I've always thought that C was
This comment confuses me: kill -l generally lists all signals. I thought that a
I am developing a comments box that will save the comment through a JQuery
I have a form that looks something like this <form action=# class=ajax_form method=post id=comment_form
Q: how to share information in nested classes about parent class class Post MODEL

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.