Additional Shell Resources

tl;dr I've learned about a couple of additional resources since writing Stronger Shell, and I wanted to call them out. I've also updated the original post.

Thanks to comments around the web and a little keener attention elsewhere, I've come across a few other good resources that I think are worth sharing. I'll add these to the original post as well, but if you already read that, I wanted to call them out specifically:

  • Unix for the Beginning Mage (pdf) is a tutorial on the basics of shell work done as a story about learning magic. It's delightful, free, and reminds me of the why's (poignant) guide to ruby (which I credit with getting me started in ruby and subsequently web development), but more practical and less overtly weird. Thanks roneesh for pointing it out.
  • Bash (and other shells) has useful options that may make catching errors easier — for example, set -o errexit will "Exit immediately if a command exits with a non-zero status," which can be helpful in testing (and in real scripts). For the full list of options, see the set help page in bash or see the the options index for zsh. Thanks to bdunbar for the reminder.
  • For contextual help at the commandline itself, there's a neat python app: search cmd. It lets you do things like searchcmd find "sort files by size" and will return relevant examples from places like stack overflow. Being able to search without leaving the terminal (and see the results summarized) nicely helps keep context, which is key for feeling productive. Thanks to Pycoders Weekly, which is a generally useful resource.
  • If you're looking for more examples of interesting people things do in their shell, commandlinefu has tons. Like any large repository, the quality is variable, but there's definitely lots of interesting snippets in there that can help you expand how you think about your shell.

Stronger Shell

tl;dr Shell scripting is strange and somewhat forbidding, but will serve you better than most other frameworks or languages you can learn (after your first). I'll explain how I improved and offer resources to improve yourself.

Motivation

Part of why I love programming is being able to automate repetitive tasks. That's pretty common for programmers; as the quip goes, I'll happily spend 99 hours to automated a task I could do by manually in 100. Strangely though, until I'd been programming professionally for a more than a few years, I was fine typing out many shell commands in a row every day to do repeated tasks.

As my jobs got more complicated though, my environment also did, and it became infeasible to remember everything that I needed to do in the terminal (so many server names to remember!) and too time consuming to keep searching my history all the time. Since scripting my simpler, day-to-day tasks in python wasn't really attractive, I decided to actively work to improve my shell scripting. This has paid off repeatedly.

Forcing Improvement

The trouble for me with improving at shell was bothering to start; although I knew eventually I'd get the time back, the first several scripts were going to take much longer than they'd ever save me.

Image Credit Randall Munroe / xkcd.com

Image Credit Randall Munroe / xkcd.com

Eventually, I decided to follow a rule that my friend Adam Hutton had mentioned: the second time you type a complex command in your terminal, make an alias. Aliases are quick to create and obviously save typing.

As soon as you start trying to do that though, you realize aliases, while wonderful, are pretty limited, and you stumble into shell functions and small scripts. Then, as a programmer, you are going to want flow control and variables, and that thankfully should start you hurtling through your own obsession to actually learning your shell thoroughly.

As my shell skills have improved, I've steadily widened the bar for what I will turn into a function/script/alias — I still apply the "if I type it twice, automate," but the scope of what "if I type it twice" has expanded considerably, because with scripts and functions, you can have arguments, and so automate nearly everything.

Resources for learning

Actually learning did not follow the path I expected; at first, it was hard to find recent books on bash and zsh at the right level for where I was, and shell scripting has quirks that didn't match my expectations, coming from Python/C++/Javascript. I eventually found a number of websites and books though that helped a ton, and collected a few tips below that would have sped me up considerably.

Online Resources

  • My first, favorite resource is the BashGuide wiki pages on wooledge.org, which is currently being updated to be a fuller tutorial at guide.bash.academy.
  • Joshua Levy's "The Art of Commandline" is a good, quick introduction that will get you moving
  • Explain Shell is a terrific tool for pasting complex commands and seeing what is going on with them
  • Of course, you can read man bash, which, although turgid, is full of useful wisdom. While you're reading the manual, you'll also want to read man test so that you finally understand what [ -f ~/.bashrc ] means.
  • Unix for the Beginning Mage (pdf) is a tutorial on the basics of shell work done as a story about learning magic. It's delightful, free, and reminds me of the why's (poignant) guide to ruby (which I credit with getting me started in ruby and subsequently web development), but more practical and less overtly weird. Thanks roneesh for pointing it out.
  • For contextual help at the commandline itself, there's a neat python app: search cmd. It lets you do things like searchcmd find "sort files by size" and will return relevant examples from places like stack overflow. Being able to search without leaving the terminal (and see the results summarized) nicely helps keep context, which is key for feeling productive. Thanks to Pycoders Weekly, which is a generally useful resource.
  • If you're looking for more examples of interesting people things do in their shell, commandlinefu has tons. Like any large repository, the quality is variable, but there's definitely lots of interesting snippets in there that can help you expand how you think about your shell.
  • For ZSH (which I love), there's a great bunch of examples and capabilities explained in the ZSH Lovers man page. Additionally, Nacho Caballero's Master Your Z Shell with These Outrageously Useful Tips really expanded my mind as to what was possible in ZSH, and I'm grateful that he wrote it. Via Pycoders Weekly.
  • Finally, your nearest linux or mac computer are FULL of shell scripts, and once you get past reading things like [[ -z $(ssh -T host "exit" 2> /dev/null) ]], you can learn a lot from those scripts. A good habit is to read the scripts that libraries tell you to curl and pipe to sh, like any of the scripts on curlpipe (since you don't want to just run some random shell script from the Internet without knowing what it's going to do, right?).

The other thing that may not be immediately obvious is that bash/zsh/whatever-shell-you-like is your language, and the rest of the POSIX tools are your standard library. With that in mind, it'll definitely be worth your time to pick up even a little grep, tar, awk, sed, curl, head, tail, ssh, and friends.

The DigitalOcean community has actually done a really nice job writing tutorials for awk, sed and a a fair number of other tools. For awk specifically, I'm a fan of the Grymoire awk tutorial which substantially demystified it for me.

If style matters to you (PEP8 has made it matter to me everywhere), the bash hackers wiki style guide is a helpful resource so that your scripts look fluent and avoid obvious errors. Similarly, you should setup a linter, like a linter like shellchck.

Books

No Starch Press has several excellent books for the aspiring shell user. My favorite is The Linux Command Line (affiliate link here and below, thanks!), which gives you a gloriously thorough and well-organized overview of many of the relevant shell commands you might need and, once you have those under your belt, shell scripting. If you're a bit farther along, some chapters may be too basic, but if you're like me, there are probably surprising holes in what you know that the book will fill in nicely.

Along similar lines to The Linux Commandline is Linux Command Line and Shell Scripting Bible, which is well-loved and thorough.

If you are just looking for shell scripting specifically, and you also have decided ZSH is awesome, Oliver Kiddle's From Bash to Z Shell is old but still useful; I refer to it from time to time when picking up new bits of ZSH still.

Finally, as you get more into this stuff, I highly recommend Michael W. Lucas's SSH Mastery (and honestly, any of his other sys admin books); if you're like me, you're probably spending a substantial portion of your shell time SSH'd into a remote host, and it's worth your time to really understand how SSH works and is configured.

Things that I wish I'd known

There are a couple things that I did not infer when I was initially trying to learn Bash from reading examples and experimenting, and I hope they speed someone else up in the future. They are also basically impossible to google, since they are mostly punctuation. Most can probably be picked up with a linter like shellchck, but I didn't have that setup at first either, so let my difficulty be your gain:

  • Bash is whitespace sensitive in a way that's... unexpected. You cannot have spaces around the operator in assignments (so export FOO = "bar" won't work, but export FOO="bar" is correct). You must have spaces inside your test expressions (so [[-z $FOO]] won't work; [[ -z $FOO ]] is correct).
  • There are a number of constructions that are nearly functionally equivalent. The ones that most tripped me up are:
    • [ expression ] and [[ expression ]] have the same function, and in general, if you're writing bash scripts, you should just use the [[ expression ]] version; the single [] version is older and doesn't support as many operations. And, to keep things optimally confusing, [] is the same as the test builtin. See the BashFAQ for the gritty details.
    • Single ticks (`command`) and $(command) are functionally equivalent, but in bash prefer $(command)the BashFAQ explains why.
    • Variable substitution can be done as either $VARNAME or ${VARNAME} — generally I do the latter, but you'll see both.
    • source and . do the same thing, which is execute the sourced file in the context of your current shell (so it can set environment variables in your interactive environment and whatnot).
  • Parens aren't exactly obvious either. (command) runs the command in a subshell (for uses, see the bash guide, or if you're impatient, just remember $(command) gets you the output of a command, so the command has to be running in those parens, right?). However, ((1+2)) does math (and $((1+1)) gets you the result of math). Having parens do something other than group expressions/override precedence was not something I expected.
  • Like our dear friend javascript, variables are global by default. In a function, to scope the variable to the function use the local keyword.
  • Bash (and other shells) has useful options that may make catching errors easier — for example, set -o errexit will "Exit immediately if a command exits with a non-zero status," which can be helpful in testing (and in real scripts). For the full list of options, see the set help page in bash or see the the options index for zsh. Thanks to bdunbar for the reminder.
  • If you switch between linux (GNU) and mac (BSD), many of your basic shell commands will differ; you almost certainly cannot copy some awk magic from stack overflow and expect it to work on both.

Where to go Next

The most important thing to do is write more shell code. The best way to learn is to do, and so, do! If you haven't yet, also pickup one of the great editors, either vim or emacs (or, my personal favorite, both via spacemacs) — once you're spending more time in your terminal, you'll likely want to be able to quickly pop into files to edit them, and it's nice to be able to do so right in the terminal.

Finally, make your shell environment nice. Spend some time with awesome shell (if you use bash) or awesome zsh plugins for ZSH users. Get a decent looking theme for vim. Setup nice completions for commands, and care for your dotfiles by versioning them. Spend a few hours making your environment pleasant and you'll be rewarded for years by that work.

Updates:

Version-controlling my dotfiles

tl;dr: There are lots of neat ways to version-control your dotfiles. I ended up with homeshick and myrepos.

Background

One of my goals for the last year has been to get more competent with my shell. I've used oh-my-zsh for years and found it helpful, but I'd never really bothered to learn shell-scripting or much about how my shell actually works. I've remedied that a bit (which I'll write about separately), and it's meant that my shell dotfiles have become a lot more useful to me.

Once I started to have a real investment of time in my shell and other command-line configs, I started wanting my usual tools -- version control, history, etc. I also wanted to be able to keep various computers in sync; I have a personal macbook where a lot of my configs originate, then my work computer, then various servers I log into to work periodically.

I'd gotten to a similar place a few years ago when I worked at US News, and I'd followed Brandon Rhodes's method for using git to track your dotfiles; however, my needs have changed, so I decided to see what other people do.

Goals

Since I'm doing a good bit of development at home and at work, and I want to share some settings but keep others private to their respective environments. Brandon's method of using a single git repo for all my dotfiles has a lot of benefits, but it doesn't lend itself to this particular setup; I want the work stuff in a private account and my personal stuff in my public github repo.

I also wanted to be able to maximize sharing, which means not requiring that a person take my whole setup to use part. For example, I found Byron Peebles's dotvim really helpful in starting to use vim, and I wanted to be able to share my various settings with colleagues and friends. However, I expect that not everyone is going to want to use my many idiosyncratic aliases and utilities. This means making my dotfiles modular, which is a little tricky --- it's not totally weird to want two separate repos that both end up putting files in .vim; however, doing that with git out of the box isn't really obvious.

For myself, I wanted a reasonably braindead way to bootstrap new machines and keep them in sync. It's not too unusual that I'll be doing a lot of work on a server for a few weeks and then stop touching it for months, only to login again and be frustrated that I've evolved how I work on my laptop and can't use similar shortcuts on the new box. Similarly, I tend to "clean home" every few months and then just work with what I've got for a while, touching-up the edges for a year or two before doing some serious remodeling. That means I need to be able to automate a lot of how my setup works so that it "just works" during the time I'm not focused on sharpening my tools.

The tricky part is that adding modularity makes the bootstraping/sync issue harder; I don't want to have to remember which 10 git repos to pulldown or update on a given box. I played a little with using submodules, but as always happens to me, I ended up making a little mess of my submodules within a few days; this reminded me that that tool doesn't mesh well with how I work.

Finally, because where I can, I try to use the most-specific tool to manage dependencies. For example, in a python project, you could use git submodules and shell scripts to manage all your dependencies, but pip has always been a lot less annoying for me. Similarly, vundle in git handles more than just checking-out and updating git plugins.

Where I ended up

It turns out, there are a ton of tools for managing your dotfiles, and github has even curated them. Initially, I was really interested in rcs and the python dotfiles package. But with rcs, my modularity goals were tricky to think through, and with the python package, given that I want all my python packages in virtualenvs, and part of that setup depends on my dotfiles, I decided to try to keep things simpler if I could.

I ended up using a set of shell scripts called homeshick to manage the dotfiles themsleves and myrepos to manage the many git repos. Homeshick handles symlinking files from a gitrepo into your home. It depends on bash, which means I can just clone it and be on my way. Symlinking has its own downsides, but the approach homeshick takes allows you to have a bunch of repos that all link into the same directories in your home, which solves my modularity problems. Then, myrepos handles cloning and bootstrapping the several repos where I keep things. I can have separate configs for myrepos for work and home (and even have one include the other), which is really useful.

I use these utilities to bootstrap tool-specific package managers, like zgen for manage zsh plugins or vundle for vim plugins.

What's cool

Now, I can run mr --trust-all bootstrap https://raw.githubusercontent.com/mattbowen/dotmyrepos/master/home/.mrconfig in my home and have all my basic tooling setup. Not only does this pull my personal tools, but also external dependencies like homeshick or spacemacs (which I've been playing with). Because myrepos is well-designed, at work I can have a .mrconfig that includes my home one and extends it. This allows for the sort of modularity and layering that I wanted.

I was able to pretty easily separate out my personal base setup from work-specific settings and keep all of this in sync. I can run homeshick pull and mr pull and have my tooling updated.

Also, with automated-setups, I'm starting to think about ways to make my own tooling more useful; for example, since I can create more "layers" of setup, I might want to break my .vimrc into some basic settings I want absolutely everywhere (like, even on my router), and then all the plugins I like using for programming and writing. I've got to think on this more though, since it will add complexity that I need to remember later.

What's uncool

First, I'm using many tools; maybe too many. The domain-specific plugin managers are useful since they usually do more than just manage git repos, but they also mean thinking about a bunch of tools in a specific order if I want to make sure I'm using the same vim plugins everywhere. This means it's not braindead enough yet; for example, if I've added some new zsh plugins, it's not always even obvious to me which tools I should run in which order to make sure they're updated in a given environment. I should be able to solve this with a shell-script or two, but I haven't done so yet (I didn't think of it until writing this, so +1 blogging I guess).

Having so many repos also makes sharing somehow harder; it's hard to point people to any one specific place if they want to use my tools. This blog post is in part to help with that, but I probably need to write another once my setup has settled down a little more to give a better overview.

Also, the homeshick approach of using symbolic links for everything means it can be hard to tell at a glance if I have untracked configs sitting around in my home. This is another thing that I can almost certainly fix with some shell scripting, but I haven't yet, and it's another place I can trip. Then there's all the problems that come with symbolic links generally; part of why I picked homeshick is that they have documented how to work around some of these problems, but just reading the docs should give you an idea of the woes you're headed for with the symlinking approach.

Finally, ironically, homeshick itself makes it slightly harder to share my configs, since homeshick requires that all the configs be in a REPO/home layout, which I don't think other people necessarily want. But, outside of work (where I don't think people will really mind), I don't really expect people to "fork" my dotfiles as much as copy them and use them for their own purposes, in which case, a slightly-weird directory-layout isn't so bad. And, on the positive, it makes it obvious where to put docs, if I ever write any.

What's next

Foremost, I need to just live with this for a few more weeks; I'm pretty happy with how things are working, but only time will tell whether the complexity is really worth it.

In the meantime, I want to smooth out some of the "what's uncool" — the biggest things are

  • making it easy to get an environment totally updated without having to run a ton of commands
  • being able to see which dotfiles aren't currently tracked somewhere
  • coming up with some sort of documentation for the various "castles" (homeshick's name for a set of tracked files) I have tracked

Resources and my repos

As I worked through this, I found the following really helpful:

I've got the following repos available for now:

  • .zsh -- zsh plugins and customization. This deserves its own blog post, because there's a fair bit of work in here.
  • .vim -- a (now pretty distant) fork of Byron Peebles's vim config, with about a million plugins, because I sure do like plugins
  • .myrepos -- the home of everything else
  • .git -- basic git configs and Tim Pope's automation for generating ctags

I still need to get my screen and tmux configs and a few others in, but they'll come before long.