Something I found a little repetitive was the process of setting things up to work on an exercise. The process went something like this:
- open the exercise in the browser
- copy the download command (e.g.
- cd into the correct folder (e.g.
npm installso that the tests are ready to be run
Pretty straightforward, but also a predictable and repeatable pattern... perfect for a bash function! Here's the command I want to be able to run in order to make all of the above execute (where the name of the exercise is 'collatz-conjecture'):
$ devil collatz-conjecture
To make this possible, here's the bash function that I added to my
.zshrc (I use zsh so added it to my
.zshrc, but if you're using bash, add it to your
Let's break it down. On line 1 is the name I've given to the function (devil), followed by parentheses and an open curly brace (standard function syntax). I named my function 'devil' because it's easy to type, and something I associate easily with 'exercism' (making it easy to remember).
Line 2 is where the awesomeness happens. These are all the steps I was previously doing 'manually', run for me by executing only one command. The
&& between each command means that each command must succeed in order for the next one to execute. This makes sense in this context, because each command relies on the previous commands's success. For example, we can't cd into the folder in step 2 if it wasn't created in step 1. But what about the
$1? That's the bash way of saying "grab the first argument passed in when the function is run, and use it here". So in our example above,
$1 would hold the value
Line 3 is the closing brace to end the function.
So now all I need to know is the name of the next exercise I want to attempt on Exercism.io, and I can simply run
devil exercise-name to have my bash function do all the setup work for me!
Here's what it looks like in action:
If you've found a typo, a sentence that could be improved or anything else that should be updated on this blog post, you can access it through a git repository and make a pull request. Instead of posting a comment, please go directly to https://github.com/biancapower/my-dev.to and open a new pull request with your changes.