The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 04-23-2009
EagleFlyFree EagleFlyFree is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 13
() executes the statements in a new subshell, with separate state. {} executes stuff in the current shell.

Example:

Code:
(aVariable="hello"); echo $aVariable
this doesn't print "hello" because the variable was assigned inside a new shell, whose state was discarded when the () expression ended. Think of variable scoping in C; variables live and die inside the block where they're declared.

Code:
{aVariable="hello"; }; echo $aVariable
this prints "hello" because the variable was assigned in the same shell as the next statement.

It's the same difference as:
Code:
sh myScript.sh
and
Code:
source myScript.sh


Also, yes, you need a semicolon to end the last statement inside {}; that's how the shell's grammar is defined.
Kind of how you can either do this:
Code:
if $condition; then $statements; fi
or this, using newlines instead of semicolons to separate the syntax parts:
Code:
if $condition
then
    $statements
fi
According to the bash man page, it's different from () because { and } are reserved words instead of metacharacters, which means they don't automatically cause word breaks. Presumably the same applies in the rest of the shells.

Last edited by EagleFlyFree; 04-23-2009 at 02:14 PM..