Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


My files are / / outputs
file exists
file moved outputs
file copied
file emptied runs the scripts and

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

share|improve this question
Try running each command piping it through sed "s/\^/command1 /" – j_kubik Jul 23 '13 at 11:08
Give me please an example with the information that I give. I don't really understand the sed functionality. I'm sorry. – Ivan Dokov Jul 23 '13 at 11:18
up vote 10 down vote accepted

I assume that what you are doing in your is:

Just relace it with | sed "s/^/[command1] /" | sed "s/^/[command2] /"
share|improve this answer
This works perfectly for my case. Thanks! – Ivan Dokov Jul 23 '13 at 11:38
+1 for simplicity. – Daniel Andersson Jul 23 '13 at 11:41

A minimal example of

for i in command{1,2}.sh; do
    ./"$i" | sed 's/^/['"${}"'] /'

With and executable and in the same directory just echoing the wanted strings, this gives the shell output:

$ ./ 
file exists
file moved
$ ./ 
file copied
file emptied
$ ./ 
[command1] file exists
[command1] file moved
[command2] file copied
[command2] file emptied

Quick sed breakdown

sed 's/^/['"${}"'] /'
  • s/ enters "regexp pattern match and replace" mode
  • ^/ means "match the beginning of every line"
  • ${} happens in the shell context and means "$i, but strip the suffix .sh"
  • ['"${}"'] / at first prints a [, then exits the quoted context to grab the $i variable from the shell, then re-enters to finish with the ] and a space.
share|improve this answer
Thanks for the clarifications. Your answer was helpful indeed, but @j_kubik's example was just the one I need. – Ivan Dokov Jul 23 '13 at 11:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.