View the newest xkcd comic.

xkcd(){ wget -qO- http://xkcd.com/|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}
This function displays the latest comic from xkcd.com. One of the best things about xkcd is the title text when you hover over the comic, so this function also displays that after you close the comic. To get a random xkcd comic, I also use the following: xkcdrandom(){ wget -qO- dynamic.xkcd.com/comic/random|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}

10 Alternatives + Submit Alt


  • 6
    eog `curl -s http://xkcd.com/ | sed -n 's/<h3>Image URL.*: \(.*\)<\/h3>/\1/p'`
    bluesman · 2010-08-31 13:23:21 0

  • 1
    wget `lynx --dump http://xkcd.com/|grep png`
    metalx1000 · 2009-12-03 23:10:22 0
  • I wasn't sure how to display the image, so I thought I'd try xml for a different twist. Show Sample Output


    0
    curl -s 'xkcd.com' | awk -F\" '/^<img/{printf("<?xml version=\"1.0\"?>\n<xkcd>\n<item>\n <title>%s</title>\n <comment>%s</comment>\n <image>%s</image>\n</item>\n</xkcd>\n", $6, $4, $2)}'
    putnamhill · 2009-12-03 19:49:37 0
  • This function displays the latest comic from xkcd.com. One of the best things about xkcd is the title text when you hover over the comic, so this function also displays that after you close the comic. To get a random xkcd comic use the following: xkcdrandom() { wget -qO- http://dynamic.xkcd.com/comic/random | sed -n 's#^<img src="\(http://imgs.[^"]\+\)"\s\+title="\(.\+\?\)"\salt.\+$#eog "\1"\necho '"'\2'#p" | bash; } These are just a bit shorter than the ones eigthmillion wrote, however his version didn't work as expected on my laptop for some reason (I got the title-tag first), so these build a command which is executed by bash.


    0
    xkcd() { wget -qO- http://xkcd.com/ | sed -n 's#^<img src="\(http://imgs.[^"]\+\)"\s\+title="\(.\+\?\)"\salt.\+$#eog "\1"\necho '"'\2'#p" | bash ; }
    John_W · 2010-08-25 15:44:31 3
  • Same thing just a different way to get there. You will need lynx


    -1
    lynx --dump --source http://www.xkcd.com | grep `lynx --dump http://www.xkcd.com | egrep '(png|jpg)'` | grep title | cut -d = -f2,3 | cut -d '"' -f2,4 | sed -e 's/"/|/g' | awk -F"|" ' { system("display " $1);system("echo "$2); } '
    solarislackware · 2009-12-03 18:53:57 0

What Others Think

How do you execute it? I got this error: local: 1: ?>: bad variable name when I executed it like this: local f=$(curl -s http://xkcd.com/);display $(echo "$f"|grep -Po '(?<=")http://imgs.xkcd.com/comics/[^"]+(png|jpg)');echo "$f"|awk '/<img src="http:\/\/imgs\.xkcd\.com\/comics\/.*?" title=.*/{gsub(/^.*title=.|".*?$/,"");print}';
andresmh · 617 weeks ago
You execute it by pasting in the command as is on the command line (which defines a function called xkcd). Then you use it by simply writing "xkcd" on the command line.
anders · 617 weeks ago
aha! thanks! :) how do you set it so it's always available, perhaps with a keyboard shortcut
andresmh · 617 weeks ago
You can make it always available by pasting it in your .bashrc file.
eightmillion · 617 weeks ago
Remember this does not work on ZSH for some reason.
matthewbauer · 616 weeks and 6 days ago
Zsh doesn't allow declaring a variable local and assigning it at the same time, whereas bash does. This function can be made to work with zsh by separating the declaration and assignment like this: xkcd(){ local f;f=$(curl -s http://xkcd.com/);display $(echo "$f"|grep -Po '(?<=")http://imgs.xkcd.com/comics/[^"]+(png|jpg)');echo "$f"|awk '/<img src="http:\/\/imgs\.xkcd\.com\/comics\/.*?" title=.*/{gsub(/^.*title=.|".*?$/,"");print}';} And the same for the random version: xkcdrandom(){ local f;f=$(wget -q http://dynamic.xkcd.com/comic/random/ -O -);display $(echo "$f"|grep -Po '(?<=")http://imgs.xkcd.com/comics/[^"]+(png|jpg)');echo "$f"|awk '/<img src="http:\/\/imgs\.xkcd\.com\/comics\/.*?" title=.*/{gsub(/^.*title=.|".*?$/,"");print}';}
eightmillion · 616 weeks and 5 days ago
Cute :]
thebodzio · 616 weeks and 5 days ago
lynx --dump --source http://www.xkcd.com | grep `lynx --dump http://www.xkcd.com | egrep '(png|jpg)' | awk ' { print $0 } ' ` | grep title | cut -d = -f2,3 | cut -d '"' -f2,4 | sed -e 's/"/|/g' | awk -F"|" ' { system("display " $1);system("echo "$2); } '
solarislackware · 616 weeks and 1 day ago
Debian doesn't compile grep with the '-P' option; I've altered your command to use 'pcregrep' instead, which will work (I did have to use apt-get to install pcregrep). xkcd(){ wget -qO- http://xkcd.com/|tee >(feh $(pcregrep -oo '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|pcregrep -o '(?<=(\w{3})" title=").*(?=" alt)';}
bartonski · 607 weeks and 6 days ago
Thanks bartoniski. I usually try to include a version that doesn't use Perl compatible regexs, but didn't for this command. Grep's about the first thing I replace when I install Debian.
eightmillion · 607 weeks and 6 days ago
A version that works in zsh and takes args for numbered comic, random comic, or leave blank for newest comic: xkcd() { local comic="$1"; local xkcdurl; if [[ $comic == "random" ]]; then xkcdurl="http://dynamic.xkcd.com/comic/random/"; else xkcdurl="http://xkcd.com/${comic// /+}"; fi; wget -qO- ${xkcdurl}|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}
zoqaeski · 578 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: