6

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?

2 Answers 2

6

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.

6

As always in Unix, there are options.

paste

Use the paste utility with a blank LHS file, for example:

cat ~/.bashrc | paste /dev/null -

The cat command is a placeholder for your second script.

The paste command is designed to take two files and put them together, e.g.:

$ paste file1 file2
file 1 line 1    <TAB>  file 2 line 1
file 1 line two  <TAB>  file 2 line 2
file 1 line 3    <TAB>  file 2 line iii

The way I'm using it above is to use /dev/null as file1, and STDIN as file2, specified by -. When used as input, /dev/null returns NULL characters. This means every line of file2, the output of your second script, is preceded by NULL followed by a TAB character.

You can go further: paste has a --delimiter option, but specifying two spaces doesn't give the expected effect: delimiter 1 is used between the first and second columns, delimiter 2 is used between the second and third, and so on.

paste|expand

To get a two-space indent, you would use the plain paste piped again through expand -2: this turns all tabs into two spaces:

cat ~/.bashrc | paste /dev/null - | expand -2

This will behave exactly as you specified.

sed or awk

Yet another approach is to use sed or awk:

cat ~/.bashrc | sed 's/^/  /'

This searches for the beginning of line ("^"), and substitutes, or really inserts a pair of spaces.

cat ~/.bashrc | awk '{printf "  %s\n",$0}'

This takes each full line ("$0") and formats it with printf, using the format specifier of two spaces, followed by the string to be printed, followed by a newline.


Bear in mind all of the above commands can eliminate the cat piece of the pipeline, i.e. paste /dev/null ~/.bashrc, or paste /dev/null ~/.bashrc|expand -2, equally sed 's/^/ /' ~/.bashrc or awk '{printf " %s\n",$0}' ~/.bashrc. It's often taken as a beginner error to use cat first in a pipeline.

2
  • 1
    +1 for a cleaner use of sed
    – Jim L.
    Dec 11, 2018 at 21:18
  • 1
    @JimL. I just realized I'm committing the heinous sin of cat ... | <command> where paste, sed, and awk all take a filename as an argument. hangs head in shame In my defence, the answer to "what was I thinking?" appears to be clarity, so that the filename is always in the same place whichever alternative is used.
    – Rich
    Dec 13, 2018 at 20:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .