Linux – Shell Script output indentation

formatlinuxshellunix

I want to call a shell-script / application inside another script. Every line of the contained script should be indented by 2 spaces. Is this possible?

The output should look something like this.

I'm the main-scripts' output.
  I'm a second script, called inside the main-script.
  Every line of my output is indented by 2 spaces.
  This is not implemented inside of me, but in the main-script
  as I should also be called outside of the main-script and then
  my output shouldn't be indented.
Thats all from the second script.

Is this possible and how?

Best Answer

You could use sed or awk to do this. For example in your main script you could do:

# execute the command and pipe to sed
second-script | sed 's/\(.*\)/  \1/'

The above sed command simply prepends two spaces to each line of output from second-script.

Related Question