Use Twitter from the Terminal (part 1)

I know that this has been done to death, but I felt that I should persist in beating a deceased horse. When I am at work and don’t want it to be obvious that I am on Twitter, I use this nifty little Terminal trick that lets me send out tweets. On my laptop, I took it a step further and wrote a command-line Ruby script that does that and more.

Here are the basics of posting to Twitter through the Terminal. Somewhere on your computer, create a new shell script file. In my case, I will call it twitter.sh. Type this into the new file:

#!/bin/bash
curl -u username:password -d status="$1" http://twitter.com/statuses/update.xml
echo Message sent.

Now it is as simple as running a shell command with your message attached.

./twitter.sh "Hey, I am screwing around at work!"

It is as simple as that. You can create shortcuts that make using this even easier. The most common thing I do when I write this script is to alter the .bash_profile file to save myself the trouble of typing out ./twitter.sh.

Open a new terminal window (or tab) and enter “nano .bash_profile”. (It is not necessary that you use nano, I just prefer it for quick text editing in the Terminal.) Add the following to a new line to the file with the path in quotes being the path to your twitter.sh file and save.

alias tw="~/path/to/twitter.sh"

Now close that Terminal tab and open a new one. That is the only way to get the newly edited .bash_profile to load and your new alias to take effect. To send a message use your alias in place of ./twitter.sh.

tw "I am screwing around at work more efficiently!"

You’re welcome.