Hacker News new | past | comments | ask | show | jobs | submit login
Shell [Scripting] Style Guide (google.github.io)
39 points by denvaar 60 days ago | hide | past | favorite | 6 comments

This pairs extremely well with dylan's Pure Bash/sh Bible [1], both, combined with admittedly way too many hours browsing random people's dotfiles can exponentially increase your skills with shells scripting.

Some tips of my own:

Adding `2>/dev/null` or `>/dev/null 2>&1`after most commands with expected or just plain useless output can make writing headless scripts very easy, it also comes in very handy when trying to shut up some basic utilities.

A handy function:

  execute() {
 pkill -x "$1"
  sleep 0.1
  "$@" &
  } >/dev/null 2>&1
`set --` before running a command can be used to remove all excess whitespace (making parsing dead easy).

And last but not least always using printf instead of echo makes your scripts 1000x cooler :)

0. https://github.com/dylanaraps/pure-sh-bible 1. https://github.com/dylanaraps/pure-bash-bible

Why is printf better than echo?

Following a style guideline is really important in scripts. But, having your scripts checked by ShellCheck [1] is really mandatory. Can’t imagine to write any script without it.

[1] https://www.shellcheck.net/

What’s the step up in terms of “structured languages” from bash. My shell scripts are getting longer as I become more competent. The one I am aware of is perl, any others?

I think the next go-to is Python; you can go as in-depth as you want with it.

Ha, I use python daily but did not think of it.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact