Sven Bendel
A droid's life

A droid's life

Bring some zen to your shell

Bring some zen to your shell

Sven Bendel's photo
Sven Bendel
·Apr 9, 2019·

2 min read

Sifting through the web I stumbled upon a little API called zen which is provided by the guys over at GitHub. The API returns a random (mostly developer focused) piece of wisdom and is accessible via https://api.github.com/zen .

I thought it might be nice to have such a thing every time I open my shell (I am using iterm2 with zsh for what it’s worth, but the following will work with nearly every shell out there).

Screenshot of a fresh terminal session with a zen quote

To make it happen first locate your shell’s configuration file. Usually you can find those in your home folder, oftentimes they start with a dot (.) and end with rc. In the case of zsh the file is called .zshrc. In case of bash (the default shell on many systems) it is .bashrc. If the file doesn’t exist yet simply create it in your home folder.

TL;DR

To get exactly the output like in the screenshot above you would append the following two lines to your rc file:

echo "Welcome, commander!"
curl -s -m 1 -w '\n' https://api.github.com/zen

And here is what we have to do in detail

To output something at every start of the shell just add a line at the bottom of the configuration file. E.g. to print a simple static text we would append:

echo "Welcome, commander!"

To print a quote from the zen API we need to use curl instead which is a pretty common way of performing network requests via the shell:

curl -s -m 1 https://api.github.com/zen

-s is short for --silent and does just that: it won’t show any errors or timeouts or request-meta information like status codes, just the plain response.

-m 1 controls the timeout: In our case we want it to be rather small as usually you fire up the terminal quite often and want it to be available as fast as possible. So this sets the timeout to 1 second.

Finally -w '\n' takes care of adding a newline character at the end of the curl response. Otherwise some shells (like zsh) would print out a percent sign at the end of the line.

And that’s it. Enjoy the zen. ☯️

Did you find this article valuable?

Support Sven Bendel by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this