Grouping commands in the Bourne Shell (sh)

Parentheses are used to run commands in a subshell:

$ foo=bar
$ echo $foo
bar
$ (foo=superbar; echo $foo)
superbar
$ echo $foo
bar

Curly braces are for grouping commands into a list to run in the current shell. But be careful with the tricky syntax:

  • Set mandatory space after the opening and before the closing curly brace
  • Put a semicolon after the last command of the group
$ { foo=superbar; echo $foo; }
superbar
$ echo $foo
superbar

Bourne Shell command groupping

Advertisements

About mezzantrop

10 years of experience in large SAN and storage environments: mainly Hitachi, HP and Brocade. Now I am a proud SAN/storage IBMer. Empty – expect-like tool author. FreeBSD enthusiast.
This entry was posted in Tips & tricks and tagged , , , , . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s