Explain, from your shell

A few weeks ago, almost everyone on Twitter was sharing Explain Shell – with good reason. It’s an awesome site that you can copy and paste a command into and it’ll explain each component of it to you.

There are a few examples on the homepage, I particularly like tar xzvf archive.tar.gz and ssh -i keyfile -f -N -L 1234:www.google.com:80 host.

Now, whilst you can copy and paste a command you’re working on into the site, wouldn’t it be awesome if you could trigger it from your command line? Thanks to Schneems, you can!. I prefer the shell version, so add the following to your .bashrc (or .zshrc etc) and then next time you want to see what a command does, just add explain to the beginning of it.

function explain {
  # base url with first command already injected
  # $ explain tar
  #   => http://explainshel.com/explain/tar?args=
  url="http://explainshell.com/explain/$1?args="

  # removes $1 (tar) from arguments ([email protected])
  shift;

  # iterates over remaining args and adds builds the rest of the url
  for i in "[email protected]"; do
    url=$url"$i"+"
  done

  # opens url in browser
  open $url
}
Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Leave a comment?

Leave a Reply