Send a Twitter Message from the Unix Command Line

If you are a fan of Twitter and also a Unix user who spends most of your waking hours on the command line, wouldn’t it be nice if you can send your Twitter update from the Unix command line? Twitter has an API that makes sending from the Unix command line easy. All you need is the curl command (which should be available nowadays on most Unix systems) and you can fire up your Twitter message like this:

    curl -u mytwitterusername:mypassword -d status="the server is up!" \
                  http://twitter.com/statuses/update.json

Note: The command above should be all in one line.

Easy, isn’t it?

But you might say, “Wow, that’s a lot of typing to do. BTW, is that my password in clear text?”

Well, this is where Unix scripting comes to the rescue. You can easily wrap this in a shell script and your usage could be as simple as this:

        twitter.sh "the server is up!"

The rest are inside the script where you can hide your username and password by making sure that only you can read it. For example:

#!/bin/sh
curl -u mytwitterusername:mypassword -d status="$*" \
      http://twitter.com/statuses/update.json

If you are not comfortable putting your password and username in the script, you can always make the script prompt for them. For example:

#!/bin/sh

echo -n "Enter username: "
read USERNAME

echo -n "Enter password: "
stty -echo
read PASSWORD
stty echo
echo

MESSAGE="$*"

curl -u $USERNAME:$PASSWORD -d status="$MESSAGE" \
     http://twitter.com/statuses/update.json
echo

For details about this API, see Twitter documentation: http://apiwiki.twitter.com/REST+API+Documentation

Alvin Abad

Advertisements

6 thoughts on “Send a Twitter Message from the Unix Command Line

  1. @daniel: You do not make the tweets do that from any operating system. The twitter system does that for you – try looking at Ruby On Rails

    @Alvin: Please note that the user name and password will be visible in ps -ef output, which may be an issue on a multi user machine. If that is a worry then just omit the password in the interactive use case: curl will prompt for the password if a userid is given and a password is needed.

    If you wish to post programatically you should consider coding soemthing in ruby, python or perl to do this…

  2. does this still work? i seem to be getting an error every time.
    this:
    {“errors”:[{“code”:53,”message”:”Basic authentication is not supported”}]}

    does it need to be done using oauth keys now?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s