Use braces to group output

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2022-05-26

Say I had a bunch of distinct commands and I wanted to collect all their output in a file:

echo "Chapter 1"; echo "Chapter 2"; echo "Chapter 3" > /.all_that_was_said

Although the echoing works to display on screen, with redirection the file only contains the final bit of output, "Chapter 3"

What we need to do is group all the output together using braces:

{ echo "Chapter 1"; echo "Chapter 2"; echo "Chapter 3" } > /.all_that_was_said

This works. The file contains the output from all 3 commands.