Saturday 26 December 2009

How-To: Post to Twitter from Command-Line

This is a short tutorial explaining how to post to Twitter using command-line in Linux, without needing to even open up your web browser.

First, install the curl package:

sudo apt-get install curl

Next, create a script anywhere in your $PATH, for example twitter.sh inside ~/bin, where ~ is your home directory (make sure ~/bin is included in your $PATH variable, in case echo $PATH doesn't return it, edit ~/.bashrc and add a line like this: export PATH=/home/USER/bin/:$PATH).

The script twitter.sh should contain the following:

#!/bin/bash
curl -u USER:PASS -d status="$*" http://twitter.com/statuses/update.xml > /dev/null
echo "Message sent!"

Replace USER and PASS with your Twitter username and password, and then make the script executable:

chmod 755 ~/bin/twitter.sh

And now test it:

twitter.sh Hello, world! This is a test.

So just use it as:

twitter.sh YOUR MESSAGE

This should be all. Have a look at the screenshots below:

Running the twitter.sh script

6 comments:

Andrew Steinborn said...

You can change the script to post to Identi.ca too.

Replace http://twitter.com/ with http://identi.ca/api/ in the curl url.

Harry Simons said...

Hey, I'm also interested in knowing how to receive the tweets on my phone via sms. I already have 2 twitter accounts, one following the other and have my mobile configured in both of these accounts. What I'm seeing is, I can send the tweets from commandline (they are visible in my twitter pages), but I cannot receive the tweets on my mobile!
Can you help, please?

Senhor B said...

hi! i need a script to retrieve ALL my tweets since the first one, is it possible?
can you help me, please?
thanks in advance!

licitatii online said...

Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I'm more of a visual learner,I found that to be more helpful.

Anonymous said...

This needs to be edited/decprecated as of today as basic auth is no longer supported by the Twitter API.

Anonymous said...

I get the notification that the message is sent - but it does not appear in Twitter.