DEV Community

Cover image for Read XKCD in the terminal with some bash magic
Rob OLeary
Rob OLeary

Posted on • Updated on • Originally published at roboleary.net

Read XKCD in the terminal with some bash magic

XKCD is probably the most popular webcomic with Devs. It only seems right that you can read it from the comfort of your terminal via a xkcd command. How can we pull that off?

I use Kitty as my terminal of choice. It is fast, feature-rich, and is cross-platform (Linux and macOS). The real game-changer is that it is one of the few terminal emulators that uses GPU rendering, and has its own graphics protocol for displaying images. So, out of the box Kitty can show images (with a caveat), why not put this capability to use?

TLDR, behold the script ๐Ÿฅ

#!/usr/bin/env bash

# Save this file as "xkcd"

URL=https://xkcd.com/ 

if [ $# -gt 0 ] && [ $1 = "-r" ]
then
 URL=https://c.xkcd.com/random/comic
fi

img=$(curl -s -L $URL | 
grep 'href= "https://imgs.xkcd.com/comics.*' | 
cut -d> -f2 | 
cut -d< -f1)

kitty +kitten icat $img
Enter fullscreen mode Exit fullscreen mode

Run xkcd to fetch the latest comic, or xkcd -r for a random selection.

For a more polished version, you can clone the GitHub repo: https://github.com/robole/xkcd.

Read on if you'd like to understand the finer details!

Displaying images in the terminal

Kitty shows images via the icat kitten which is included in the default installation. A kitten is a python script to extend the functionality of Kitty.

To display an image, you can run the following command in Kitty:

kitty +kitten icat img.png
Enter fullscreen mode Exit fullscreen mode

There is a caveat that ImageMagick must be installed for icat to work. I had it installed already, so everything worked first time for me! It supports all image types supported by ImageMagick.

I believe iTerm2 has similar functionality with its imgcat script.

If you want to use another terminal emulator, maybe you can use a python library called รœberzug to achieve the same outcome. You could use one of the image viewer utilities, feh and sxiv, but they will always open a new terminal window AFAIK. Personally, I prefer to have the comic shown as an inline image, and Kitty does that with the least fuss. It's a no-brainer for my personal preferences!

Scripting time!

You should not need to install anything else if you have a *nix machine or have cygwin installed on Windows (its installed with Git usually). With the GNU coreutils: curl, grep, and cut, we should be able to get the latest webcomic from the XKCD homepage.

xkcd homepage

It would be nice to be able to read a random XKCD also. Is there a way to do this from the XKCD website?

We are in luck because the XKCD website has a Random page where they fetch a random comic from their archive for you! So, we should be the able to do the same thing with the random page and home page!

To grab the homepage, we use curl with the "-s" flag (to run in silent mode - so there is no progress meter or error messages):

curl -s https://xkcd.com/
Enter fullscreen mode Exit fullscreen mode

We can use grep to pick out the URL of the image of the webcomic. The webpage is quite short, so we wont have to write anything too gnarly. We just want to uniquely identify the image URL with the simplest regex (regular expression) possible. On inspection of the HTML, it looks like this snippet is the easiest to target:

Image URL (for hotlinking/embedding):
<a href= "https://imgs.xkcd.com/comics/rounding.png">https://imgs.xkcd.com/comics/rounding.png</a>
Enter fullscreen mode Exit fullscreen mode

There is an extra space after the equals sign for the href attribute (oddly). If we target the href attribute with the beginning of the URL, we will will get this as the only result. The regex href= "https://imgs.xkcd.com/comics.* should do the trick. We pipe the output of the curl command to the grep command and get the snippet we are after.

using grep to find the link in the webpage

Next, we want to get the URL on its own. We can use cut to strip out the unwanted parts of this string using the angled brackets as a delimiter. Using cut -d> -f2 on the output gives us "https://imgs.xkcd.com/comics/rounding.png</a", if we pipe this output to cut -d< -f1, we get the URL on its own!

So, as a single line command, we can run the following the latest webcomic:

kitty +kitten icat $(curl -s https://xkcd.com | 
grep 'href= "https://imgs.xkcd.com/comics.*' | 
cut -d> -f2 | 
cut -d< -f1)
Enter fullscreen mode Exit fullscreen mode

complete command to fetch XKCD image from homepage

Lets tidy the script up to make it more readable, and add the ability to choose a random comic with a -r flag.

If there is a "-r" argument passed as the first parameter to the script, we will fetch the page https://c.xkcd.com/random/comic instead of the homepage. Since this URL will redirect you to another page, we need to adjust our curl command to follow this redirect. We can do this by adding the "-L" flag. And thats it, the rest of the logic is the same for getting the correct image from the fetched page.

This is our final script, which we put in a file named "xkcd":

#!/usr/bin/env bash

URL=https://xkcd.com/ 

if [ $# -gt 0 ] && [ $1 = "-r" ]
then
 URL=https://c.xkcd.com/random/comic
fi

img=$(curl -s -L $URL | 
grep 'href= "https://imgs.xkcd.com/comics.*' | 
cut -d> -f2 | 
cut -d< -f1)

kitty +kitten icat $img
Enter fullscreen mode Exit fullscreen mode

Now, to get a random comic, we can run xkcd -r.

using script to fetch random XKCD comic

Mission accomplished!

Final word

You got to love the power of bash scripting for this type of task. Our basic functionality can be achieved with a single-line command, but in less than a dozen lines, we have made a tidy script for reading our favourite comic on the command-line. ๐Ÿค“


Thank you for reading! Feel free to subscribe to my RSS feed, and share this article with others on social media. ๐Ÿ’Œ

You can show your appreciation by buying me a coffee on ko-fi. ๐Ÿ™‚

Top comments (8)

Collapse
 
miguelmj profile image
MiguelMJ

Very, very cool

Collapse
 
robole profile image
Rob OLeary • Edited

Thanks ๐Ÿ˜Š

Collapse
 
robole profile image
Rob OLeary

geeky kid dancing

Collapse
 
sre profile image
SREagle

Funny enough, KiTTY on Windows is a PuTTY fork, so "kitty" is truely cross-platform g

Collapse
 
robole profile image
Rob OLeary

๐Ÿ˜‰๐Ÿ‘

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha

That's a different piece of software. Kitty, as far as I am aware, is terminal emulator for *nix based system only. I am not aware if it can be used with WSL2 or not.

Collapse
 
robole profile image
Rob OLeary • Edited

Yep, they are different. I think it was said tongue-in-cheek!

Collapse
 
kazisamir profile image
Info Comment hidden by post author - thread only accessible via permalink
KAZI-SAMIR

stackoverflow.com/questions/713376...

can some one solve the problem

Some comments have been hidden by the post's author - find out more