I've learnt the basics of programming in bash. I'd like to study a few examples of bash scripts so that I can learn good habits for bash scripting.

In particular, I'm interested in examples of bash scripts that show:

  • how to process arguments and trigger conditional functionality
  • how to check for input errors
  • how to perform command completion
  • how to organise a bash script into functions, commands, and so forth
  • how to present help messages

For example, I learnt a lot from autorandr on github. Are there any other such scripts or script repositories where I could learn about bash scripting conventions?

  • 1
    One note about where not to look: many system init scripts packaged by most distros in /etc or wherever are full of bad shell scripting practices so you should avoid using those as examples. – jw013 Oct 13 '11 at 1:33
  • Don't know why this got a downvote, +1 – n0pe Oct 13 '11 at 2:58
  • 1
    Are there "good" coding conventions for bash? The way I see it, when you use bash you're generally more interested in making it do its (extremely limited) job as efficiently and as portably as possible. If you want elegant and clean code, you use an actual programming language. – Shadur Oct 13 '11 at 7:11
  • @Shadur: there are far too many languages, so everyone is right to minimize usage of those, as in writing a Gtk3 GUI app using pure bash and bash bindings. – dotbit Dec 10 '19 at 13:37

Shameless plug, but you did ask for examples:

And if you want a good reference, use Greg's Wiki. It's a no-nonsense readable garden of chaos and beauty, with a pragmatic approach to getting things done in the best way. Just be really, really careful to investigate properly before asking anything on #bash - They can be fierce.

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.