This post was saved / ported from a previous site migration. You may encounter missing images, dead links, and strange formatting. Sorry about that!

Twitter from the command line

programming linux twitter Technology

I’ve recently started playing with twitter. A nice way to use it via the command-line (using curl) was suggested here. I have taken that and improved slightly on it.



Here is the result:

#!/bin/sh
echo -n “twitter> “
read text

while [ ${#text} -gt 140 ]; do

echo
echo “Message too long; used ${#text}/140 characters."
echo
echo -n “twitter> “
read text

done

echo
echo “Message is ${#text}/140 characters.  Press enter to post, or Ctrl+C to cancel."
read

curl –basic –user “username:password” –data-ascii “status=echo $text|tr ' ' '+'” “http://twitter.com/statuses/update.json" &> /dev/null



To use the script, copy all of that into a file somewhere in your path, then make the file executable (e.g., chmod 755 /usr/local/bin/twitter).  Now you can type ’twitter’, type in your tweet, and you’re done!

I even set up fluxbox so that mod4+t launches a terminal with the script running.  To do that, I added this to ~/.fluxbox/keys:



Mod4 t :Exec xterm -e “twitter”



If you’re not familiar with ‘mod4’, it is the Windows key on most PC keyboards.

I’ll eventually get around to writing a slightly more full-featured twitter updater in c or c++.  Until then, enjoy this script!