I read an article over at OS X Daily about posting a tweet from the command line using the twitter API. Taking this one step further here’s a handy little script that’ll even stop you posting nothing and stuff that’s too long.


#!/bin/bash
TWEET=$1
TWEETLEN=${#TWEET}
if [ $TWEETLEN -eq 0 ] || [ $TWEETLEN -gt 140 ]; then
	if [ $TWEETLEN -gt 140 ]; then
		let EXTRA=$TWEETLEN-140
		echo "Usage: tweet \"message\" (140 chars or less, you're $EXTRA over)"
	else
		echo "Usage: tweet \"message\" (140 chars or less)"
	fi
	exit 1
else
	curl -u username:password -d status="$1" http://twitter.com/statuses/update.xml
fi
exit 0

Save the file as “tweet” and make sure it’s in the path with executable permissions of 755.

chmod 755 tweet