Tweeting From the Command Line

A recent tweet from Tim Bray contained a link to an article explaining how to tweet from the command line. That prompted me to write a quick script to make tweeting from the command line a bit easier. You can can download the script: tweet. You can grab it from GitHub:

git clone git://github.com/MilesZS/command-line-tweet.git

You can see the script below:

\#\!/bin/bash \# Author: Miles Z. Sterrett \<miles -dot- sterrett AT gmail -dot- com\> \# Created: 03-29-08 \# Updated: 03-29-08 \# Sends an update to Twitter \# Uncomment and enter your own Twitter info \# to avoid having to use the command line parameters \#USER=username \#PASS=password function force\_response {   message=$1   local answer=   read -e -p "$message" answer   while \[ -z $answer \]; do     echo "You must enter a value to continue..."     read -e -p "$message" answer   done   eval $2=$answer } while getopts "u:p:hm:" OPTION do   case $OPTION in     u)        USER=$OPTARG       ;;        p)        PASS=$OPTARG       ;;        h)        usage          exit 1          ;;        m)        MESSAGE=$OPTARG       ;;        ?)        usage          exit          ;;      esac done shift $(($OPTIND -1)) MESSAGE=$1 function usage {   cat \<\<EOF   Usage: $0 \[-h\] \[-d DOMAIN\] \[-e EMAIL\] \[-u USER\]   This script will post a message to Twitter   -h            This usage information.   -m            Your message   -p PASS   Twitter password   -u USER       Twitter user name   EOF } if \[ -z $USER \]; then   force\_response "Enter your Twitter user name: " username   USER=$username fi if \[ -z $PASS \]; then   force\_response "Enter your Twitter password: " password   PASS=$password fi if \[ -z "$MESSAGE" \]; then   force\_response "Enter your update message: " message   MESSAGE=$message fi curl -u ${USER-\`whoami\`}:$PASS -d status="$MESSAGE" http://twitter.com/statuses/update.xml exit 0

I prefer to copy the script to /usr/local/bin, put my Twitter information in the script itself, and then:

tweet 'So, today, I totally wrote a bash script and stuff'

If you don’t like that, you can do something like this:

./tweet -u username -p password 'DHH and Linus are actually long lost twins.  Discuss.'

If you simply execute the script, it should prompt you for the various required information.

I don’t claim to be the most skilled bash scripter on the Internets. I encourage you to let me know what you think about it, fork it and improve it, or steal it and tailor it to your use.

Also, I am well aware that I spent more time on this than simplifying that curl call was really worth. I loved every freakin’ minute of it, too.

Note: Some functions in this script, as well as much of the style, is courtesy of browsing scripts written by my friend Aaron Schaefer. So, thanks, Aaron!