annabunches.net/content/posts/2009-07-02-twitter-from-command-line.md

2.0 KiB

excerpt_separator category layout title date author tags modified_time blogger_id blogger_orig_url
<br/> technology post Twitter from the command line 2009-07-02T11:38:00.000-04:00 Anna Wiggins
programming
linux
twitter
Technology
2013-10-22T11:19:50.705-04:00 tag:blogger.com,1999:blog-4209116010564764361.post-6156350980105195782 http://www.stringofbits.net/2009/07/twitter-from-command-line.html

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!