Clean up lint

Fix issues identified by shellcheck
<http://www.shellcheck.net/about.html>:
* Unquoted expansions (not all caused shellcheck to emit a warning)
  <https://github.com/koalaman/shellcheck/wiki/SC2086> and
  <https://github.com/koalaman/shellcheck/wiki/SC2046>
* Use $(...) instead of deprecated `...`
  <https://github.com/koalaman/shellcheck/wiki/SC2006>

Merge one-use variable dir from the else clause into the expansion in
which it's used.