Post a Twitter Update via the Command Line

Apr 20, 2010 - 11 Comments

Terminal in OS X

You can quickly post a tweet from the command line using the curl command, all you’ll need is your Twitter username and password.


Launch the Terminal and type the following all on one line, replacing username and password with your own:

curl -u username:password -d status="your status message here" http://twitter.com/statuses/update.xml

I came across this via MurphyMac who used the command to schedule Twitter updates while sleeping (seriously), using the curl command in conjunction with the sleep command. I’m not sure how practical tweeting while sleeping is, but the ability to post a quick update from the command line is pretty handy.

If you’re so inclined, you can use the above command and make a quick bash script so that you can easily tweet from the command line without typing the full string. Just place the following in a text file and name it something like tweet.sh:
#!/bin/bash
curl -u username:password -d status="$1" http://twitter.com/statuses/update.xml

Be sure to specify your username and password. Then be sure to make the file executable:
chmod u+x tweet.sh
Now you’ll just need to type ./tweet.sh "I love OS X Daily" to tweet your message to the world. Thanks to Greg Mason for correcting the permissions error!

Ian Winter took the above bash script a bit further and added the ability to prevent you from posting no tweet, and a warning if a tweet is over the 140 character limit. Here is his script:
#!/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

Like before, edit your username and password, and save the file as tweet and be sure to make it executable chmod 755 tweet

.

Related articles:

Posted by: David Mendez in Command Line

11 Comments

» Comments RSS Feed

  1. tweeter says:

    Basic authentication is not supported anymore… You need to simulate a normal login. This script currently works:

    http://360percents.com/posts/command-line-twitter-status-update-for-linux-and-mac/

  2. Will K says:

    Looking to use this to automate kiln temp status reports, but getting same error as others:

    Basic authentication is not supported

  3. Dan L says:

    I get the same error code 53: Basic authentication is not supported

  4. Gleato says:

    I’m having errors, I keep getting

    Basic authentication is not supported

    What can I do to change it from Basic authentication?

  5. […] you like this, why not learn to post tweets from the command line too? Have […]

  6. Steven says:

    if you make the bash script executable and put it in your bash_profile file as an alias you can just call it directly instead of executing the script like ./tweet.sh

  7. Andrew says:

    I added a “> /dev/null” to the end of curl line to avoid having a screen full of XML.

  8. […] 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 […]

  9. Sugar says:

    if you make the bash script executable and put it in your bash_profile file as an alias you can just call it directly instead of executing the script like ./tweet.sh

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site