linux – Bash script to automate dev environment setup


First of all, I would suggest using shellcheck when writing bash https://www.shellcheck.net/, it will point out many many errors, some trivial and some not so trivial.

Line 7:
mkdir $HOME/GoProjects
      ^-- SC2086: Double quote to prevent globbing and word splitting.

Simple enough, if your $HOME has a space in it say, the mkdir won’t work as you expect.

More subtle

Line 4:
read email
^-- SC2162: read without -r will mangle backslashes.

Maybe not a problem for you, but no harm in doing it right.

You can use shellcheck in editors via plugins etc, there’s a cli you can use. Really nice, bash is very easy to make mistakes in as we all know and this will just generally help you.

Do people organise bash scripts, yes! I’ve known some old hands who are very skilled with bash. As far as I know there’s no consensus on the structure to follow, but generally I think you want to be following good programming guidelines, split things into functions, make it modular, etc. It’s good that you have comments, but I think better still to replace the comment with a function with a sensible name which does the part the comment alludes to.

That being said, what you have is basically as far as I’m concerned one of the canonical applications of a bash script, i.e. not doing much complicated, just running a bunch of commands in order. You probably use it one off infrequently. Is it worth making it any better? Probably not.

You have no error handling, and various things are hard coded assumptions about the machine you will run this on, your script isn’t idempotent, when something does go wrong, when you run it the second time it may have funny results. Idempotency is another good thing to aim for. Is that any of this is missing bad, not really.

Personally, I don’t like bash scripts. I prefer to use a programming language which is a bit more verbose but allows me to be more confident of what I’m doing. For this reason I write my scripts in node or python when I can, as these allow me to accumulate a collection of functions which I find easier to reason about, do error handling with, and externalise configuration.