Front-end Developer. Digital hermit. Ersatz cartoonist. All-around average dad.

Wordnik Back in the Shell

By

Bringing back a forgotten and silly terminal command.

I forgot about this terminal command I set up eight years ago ?! I remember something breaking in my setup and never putting this back in place but while finally setting up a syntax highlighter for my site, I looked at this old post to see how it looked. I decided to bring this back just for the fun of it. Piping this through cowsay and making a stegosaurus in a hat say things in the terminal always makes me smile.

So here's the new version of my Wordnik API integration whereby I can quickly look up a word from the terminal.

function def() {
  params="$@"
  params_encoded=${params/ /%20}
  curl --silent "https://api.wordnik.com/v4/word.json/$params_encoded/definitions?api_key=$WORDNIK_KEY" \
  | jq -r '.[0]?.text // "Hmm … I don'\''t know that word. You sure you spelled it right?"' \
  | sed 's/<[^>]*>//g' \
  | cowsay -f stegosaurus
}

The differences:

  1. Wow. This was so old that it still used http instead of https.
  2. I'm now using jq instead of json, which — you know what? I'm not sure what json was. But it's no longer there, and since it was being used to parse the json response from Wordnik, I've had to find another solution. Homebrew's jq seems to be all the rage (brew install jq on the Mac terminal). (Check out Homebrew if you're not familiar.)
  3. So naturally that changes things. the -r flag says we want raw output, so drop the quotes, and then just get the text content of the first item returned. If we don't get a definition the alternative operator (//) lets us use a fallback response.
  4. Finally, I added my favorite cowsay, because everytime I type this, I pipe it myself.