I realise that this question may be down to personal preference but I'm pretty new to Bash / shell scripting so thought it'd be worth some research to see if there's some sort of standard/best practise or agreeable consensus.

I'm working on a few scripts that I'd eventually like to release open source (probably MIT license) so I'd like the scripts to be "done right", if there is such a thing.

The script in question has to produce an output text file from a template using variable substitution/expansion to 'fill in the blanks' as it were. At present I am using the heredoc method but I'd like to preserve TAB indents, both in my script and in the output (for readability).

Whilst I have achieved this to a certain extent using cat <<-EOF to strip the leading tabs (from my script) and by using a variable to store the TAB space (e.g. T="$(printf '\t')") which I then reference with $T which is then interpreted correctly within the heredoc back in to a TAB character, I can't help but think if an external file would just be simpler?

What do any of you more experienced bash scripters can say about it? Which of those practices is better when it comes to the code readibility/maintanance?

Here's a quick example of what I mean...

my_function() {
    T="$(printf '\t')"
    cat <<- EOF
        This is my template for $something
        $T  I'm indented with the TAB space variable...
        $T  ...but actually I'm indented twice because the "<<-" only strips the 'leading' tabs
        There's something else that I need, but it might be \${printed_literally}/$or_expanded which is cool!

It's nothing special really, I'm not looking for a 'solution' as such, more just some guidance on which solution to use -- i.e. heredoc in the script or an external template file?

  • Are you talking about self-modifying code, or about writing your own formatting language. We may need more details in order to give a good answer. Maybe the shortest sample input and shortest sample template? Sounds interesting, whatever it is. – GlenPeterson Aug 23 '12 at 18:49
  • Thanks @GlenPeterson, I think it's perhaps a lot simpler than your giving me credit for. It's basically shell variable substitution to put certain text in to specific places in the template (think mail-merge). But hopefully the example I've added will show you what I mean. – Chris Aug 23 '12 at 19:53
  • 1
    For simple scripts I'd recommend just keeping it together in one file. If something is large enough that bash is becoming cumbersome you should probably look into using something like Python or Perl. – Lucina Sep 30 '12 at 2:33

It seems simpler to include all the text relevant to the script inside the script itself, inside the heredoc, as you did.

Bash scripts are meant to be glue code, nothing too serious and complicated. If your script is getting serious or complicated, that's a sign to consider using a more advanced scripting or programming language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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