title: Bash script, execute command prefixing every line with a given string
body: <p>Let's suppose I've a bash script like the one below:</p>
  
  <pre><code>#!/bin/bash
  echo "start"
  do something
  git merge blah blah
  ant build.xml blah blah blah
  echo "end"
  </code></pre>
  
  <p>Obviously the output of the git and ant command is included in the bash output like any other echo.</p>
  
  <p><strong>Is there any way to achieve something like below?</strong></p>
  
  <pre><code>start
  i'm doing something
  [GIT] git command output
  [GIT] git command output
  [GIT] git command output
  [GIT] git command output
  [ANT] ant command output
  [ANT] ant command output
  [ANT] ant command output
  [ANT] ant command output
  [ANT] ant command output
  end
  </code></pre>
  
  <p>In case some great person will have a solution I've another little question even if definitely less important.</p>
  
  <p><strong>Can i also than split the command output if the line is too long?</strong> 
  This to avoid something like</p>
  
  <pre><code>[ANT] ant command output too long command output too long command output too
  long command output too long command output too long
  [ANT] ant command output
  [ANT] ant command output
  </code></pre>
  
  <p>As i think you noticed i'm trying to tidy up the output of my shells, their are launched through Jenkins and generated by a groovy pipeline script.</p>
  
  <p><strong>@Yoory &amp; @ghoti:</strong></p>
  
  <p>Thanks both, the answers are just lovely! </p>
  
  <p>The function is amazing, but unfortunately is a bit hard for me to integrate because it has to interact with other two things that make it a bit hard to read and confusing. Below the exact code, sorry if i did not put it before:</p>
  
  <pre><code>git merge --no-ff origin/module 2&gt;&amp;1 | tee -a /merge_files/merge.log
  if [ "${PIPESTATUS[0]}" -ne "0" ];
  then
      something
  else
      other to do..
  fi
  </code></pre>
  
  <p><strong>@Yoory</strong> 
  When i try to integrate the function, putting it right before the git command,  the PIPESTATUS returns me always 0 even if the git merge fails, i'm sure it's not so hard to find a solution to it, but as you can see i'm not so skilled with bash.</p>
  
  <p>So for now i'm using the simple sed: | sed 's/^/[GIT] /' or | sed 's/^/[ANT] /'
  I tried to integrate fmt with simply piping it with the sad.</p>
  
  <pre><code>command | fmt -w 20 | sed "s/^/[ANT] /"
  </code></pre>
  
  <p>The problem is that it works with logs with normal sentences, but it does not work for long strings without space separators (for example a path) cause looks like fmt does not break it the way i'm using, i'm looking at the man to find a parameter that could help me, but i don't think i can achieve it till now.</p>
  
  <p>I did manage to let it work with fmt but fold made the magic for me!</p>
  
  <pre><code> command | fold -w 138 | sed "s/^/[ANT] /" 
  </code></pre>
  
  <p>Thanks to everyone for the help, and if you want to add any feedback i would like to hear them, code and learn!</p>