Post a Twitter update via the command line

Apr 20, 2010 - 11 Comments

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

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: David Mendez in Command Line

11 Comments

» Comments RSS Feed

  1. 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

  2. [...] 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 [...]

  3. Andrew says:

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

  4. 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

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

  6. Gleato says:

    I’m having errors, I keep getting

    Basic authentication is not supported

    What can I do to change it from Basic authentication?

  7. Dan L says:

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

  8. Will K says:

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

    Basic authentication is not supported

  9. 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/

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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