add line at top and bottom of file before sending it to pipe

Answer #1 100 %

From man bash:

(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.

So this should work:

(echo first line; cat file; echo last line) | some_command

As suggested by chepner, when you don't need a subshell you can use {} instead of (). In this case, the ; is mandatory including for the last command in the group.

In the comments Pyrolistical remembers this also works if you have an incoming pipe instead of a file:

some_program | (echo first line; cat -; echo last line) | some-other-command

As in many unix command line applications, if the filename argument for cat is - it means "read from standard input".

Tags: linuxbash

You’ll also like:

© 2022 CodeForDev.com -