Vitor Pamplona

Innovation on Vision: Imaging , Enhancement and Simulation

Twitter from Command Line using Shell Script

It turns out that it is still possible to tweet from command line without designing an oAuth client. I am using only shell script, curl and sed. The script works just like a browser, using cookies to keep users authenticated. You can download this script at github


# Behaves like a browser.
COOKIE_LINE="--cookie /tmp/cookiesShtw.txt --cookie-jar /tmp/cookiesShtw.txt --user-agent Mozilla"

# ./twitter login <user> <passwd>
# Ret = Logged in
f_login() {
# Submit login through form post.
curl -s $COOKIE_LINE --data "username=$1" --data "password=$2" --data 'commit=Sign In' \
echo "\nLogged in"

# ./twitter say <what is happening>
# Ret = Done
f_say () {

# Encode text
R=`echo "$@" | sed -e 's/ /\%20/g'`

# Get authentication code.
auth=`curl -s $COOKIE_LINE`
auth=`echo $auth | sed -n -e "s/.*<form action=\"http:\/\/\/\"\(.*\)<input type=\"submit\" value=\"Tweet\" class=\"tweet-btns\"\/>.*/\1/p"`
auth=`echo $auth | sed -n -e "s/.*<input name=\"authenticity_token\" type=\"hidden\" value=\"\(.*\)\" \/>.*/\1/p"`

# Submit tweet through form post.
curl -s $COOKIE_LINE --data "tweet[text]=$R" --data "authenticity_token=$auth" \
--data 'submit=Tweet'
echo "\nDone"

# ./twitter see
# Ret = Formatted timeline.
f_see () {
# Get the timeline
R=`curl -s $COOKIE_LINE`

# Format timeline: time \n person \n message \n\n.
R=`echo "$R" | sed -n -e "s/.*<strong><a href=\"http:\/\/\/\(.*\)\">\(.*\)<\/a><\/strong>.*/\1/p" \
-e "s/.*class=\"status_link\">\(.*\)<\/a>.*/\1/p" \
-e "s/.*<span class=\"status\">\(.*\)<\/span>.*/\1\n------------\n/p"`

echo "$R"

# Check option and take action
case $1 in
"login" ) f_login "$@" ;;
"say") f_say "$@" ;;
"see") f_see ;;
*) echo "Usage: $0 [login <user> <passwd> | say <text> | see ]"

You can download this script at github

Posted in Mar 7, 2011 by Vitor Pamplona - Edit - History

Showing Comments

this is a great script. The only " problem " I see in it is that it displays much less tweets than I see in the browser interface when I go to with Firefox or other browser. I need to figure out how to fix this. Ideas? Thanks

- - Marco

- - Posted in Jun 19, 2011 by

is possible to auto log-in into twitter from my web application. i want to set twitter cookies in my borwser from my application

- - arivarasan

- - Posted in Sep 22, 2011 by

Hey Vitor, Works nicely, I'm testing to put y a bot's tweet line the results of execution of a statistical package. I'll give you my comments on how I deploy this. Thanks a lot.

- - Diego Diaz

- - Posted in Nov 17, 2011 by

thanks for sharing...

- - 395

- - Posted in Feb 21, 2012 by

Hey vitor, since today Twitter refreshed its web interface and this script it's no longer working. could you please tell me how make it work again?

- - Diego Díaz

- - Posted in May 8, 2012 by

Add New Comment

Your Name:

Write the code showed above on the text below.