4.11. Sequences of Commands

Normally, we put one command on each line of input to the shell. If we want to run several commands right after another, use ; to separate them. Using a pair of parentheses, the output from several commands can also be grouped together to be redirected as one set of output. Effectively the same result could be accomplished by putting the commands in a shell script file.

See also

Section 5.9 and 5.10 in the book have some nice examples of this.

Execute three commands in sequence:

$ date; pwd; ls

Group the commands to one one output stream:

$ (date; pwd; ls) > out.txt

4.11.1. Conditional Execution

The following two examples use the Exit Code of the first command to conditionally execute the second command. A logical AND operator, &&, executes the second command only if the first command completed successfully.

$ gcc myprog.c && a.out

A logical OR operator, ||, executes the second command only if the first command failed.

$ gcc myprog.c || echo compilation failed