Using bash command to prefix header in stdout?

I just saw an example on a site that passed the output of an awk command to the following command:

(echo "this is a header";cat -) 

This effectively adds a title bar at the top of the output. It seems to be stdout. What is this design? With a parenthesis and then a cat? How it works, it seems quite useful, but this is the first time I see it.

+4
source share
2 answers

Using parentheses here - "Construct Grouping" - see bash reference for details

A similar design uses curly braces:

 { echo "this is a header"; cat -; } 

This is another syntax that requires spaces around curly braces and a finite semi-colony.

The difference between using parentheses and parentheses is this: the list of commands in curly braces is executed in your current shell, the list of commands in parentheses is executed in a subshell. This is important if you want the list of commands to change your environment - when you exit a closed shell, environment changes will be lost.

+3
source

- can often be used to denote stdin in command line options that take a file name argument. Note that the cat - effect cat - same as a regular cat , that is, both versions still read stdin .

Brackets force the shell to run commands in the sub-shell . (...) essentially bash -c '...' if the shell is bash . This is because in xyz | echo "..."; cat xyz | echo "..."; cat xyz | echo "..."; cat xyz output will be sent to echo and lost.

+8
source

All Articles