Linux – Prefix to each output of a command on runtime

bashbash-scriptingcommand linelinuxshell

I am trying to make a modular script. I have several scripts/commands which are called from a single script.
I want to prefix the output of each separate command.

Examle:

My files are allcommands.sh / command1.sh / command2.sh

command1.sh outputs
file exists
file moved

command2.sh outputs
file copied
file emptied

allcommands.sh runs the scripts command1.sh and command2.sh

I want to prefix each output of these two scripts like this:
[command1] file exists
[command1] file moved
[command2] file copied
[command2] file emptied

Best Answer

I assume that what you are doing in your allcommands.sh is:

command1.sh
command2.sh

Just relace it with

command1.sh | sed "s/^/[command1] /"
command2.sh | sed "s/^/[command2] /"
Related Question