Using {list} to Group Bourne Shell Commands

A lot of people know that you can group the output of a series of commands by using a subshell (). That is, instead of this:

$ date > log $ who >> log $ ls >> log

they start a subshell with parentheses:

> 
$ (date > who > ls) > log

and only redirect once to log. But a subshell takes an extra process and takes time to start on a busy system. If all you need to do is redirect output (or input) of a set of commands, use the Bourne shell's list operators {} (curly braces):

$ { date > who > ls > } > log

Notice the spaces and the extra RETURN at the end. Each command must be separated from others. You can also write (note the semicolon after the last command):

$ { date; who; ls;
}
> log

Here are two other differences between the subshell (parentheses) and list (curly braces) operators. A cd command in the subshell doesn't change the parent shell's current directory; it does in a list. Also, a variable set in a subshell isn't passed to the parent shell; from a list, the variable is passed out.

NOTE: Jonathan I. Kamens points out that some Bourne shells may run a list in a subshell anyway, especially if there's a pipe involved. If your Bourne shell works like the example shown here, it's using a subshell, too:

$ { echo frep; foo=bar;
}
| cat frep $ echo $foo $ { echo frep; foo=bar;
}
 frep $ echo $foo bar


- JP