Wordnik Back in the Shell
PermalinkBringing 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:
- Wow. This was so old that it still used
httpinstead ofhttps. - I'm now using
jqinstead ofjson, which — you know what? I'm not sure whatjsonwas. 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'sjqseems to be all the rage (brew install jqon the Mac terminal). (Check out Homebrew if you're not familiar.) - So naturally that changes things. the
-rflag 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. - Finally, I added my favorite cowsay, because everytime I type this, I pipe it myself.