help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Is there any practical difference between { } & and ( )


From: Jesse Hathaway
Subject: Re: [Help-bash] Is there any practical difference between { } & and ( ) &?
Date: Wed, 28 Nov 2018 12:12:02 -0600

On Wed, Nov 28, 2018 at 10:01 AM Peng Yu <address@hidden> wrote:
>
> Hi,
>
> Both { } & and ( ) & start a new bash process. In this sense, do they
> have any differences (other than some trivial syntax requirements like
> { } & should include the ";")? Thanks.
>
> $ cat ./main.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> set -v
> { echo  "{$BASHPID"; } &
> ( echo  "($BASHPID" ) &
> echo "$BASHPID"
> wait
> $ ./main.sh
> { echo  "{$BASHPID"; } &
> ( echo  "($BASHPID" ) &
> echo "$BASHPID"
> 23876
> wait
> (23878
> {23877

If you background a command it is executed in a subshell, but
if you execute a command in the foreground within brackets a
subshell is not created, whereas with parens a subshell is created.

#!/bin/bash
echo "MAIN PID $$"
echo 'brackets'
{ echo $BASHPID; }
echo 'parens'
( echo $BASHPID )
echo 'background brackets'
{ echo $BASHPID; } &
wait
echo 'background parens'
( echo $BASHPID ) &
wait

Output:
MAIN PID 23607
brackets
23607
parens
23608
background brackets
23609
background parens
23610



reply via email to

[Prev in Thread] Current Thread [Next in Thread]