Send tweets in command-line with Bash

One of our FAQ has described how to use cURL with cookies, here we will show how to update Twitter status from command line on Linux or Mac with Bash, this is simpler compared to the Python way using OAuth.

You can copy blow script, save it as, update the username and password, and run it like this,

$ bash "Tweet from command-line"
[+] Fetching
[+] Submitting the login form...
[+] Getting compose tweet page...
[+] Posting a new tweet: Tweet from command-line
[+] Logging out...

Content of,

#Twitter status update bot by
#Author: Luka Pusic <>

tweet="$*" #must be less than 140 chars

uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=0 #add pause between requests

if [ $(echo "$tweet" | wc -c) -gt 140 ]; then
	echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1
elif [ "$tweet" == "" ]; then
	echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1

touch "cookie.txt" #create a temp. cookie file

echo "[+] Fetching" && sleep $sleeptime
initpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "")
token=$(echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//')

echo "[+] Submitting the login form..." && sleep $sleeptime
loginpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "")

echo "[+] Getting compose tweet page..." && sleep $sleeptime
composepage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "")

echo "[+] Posting a new tweet: $tweet..." && sleep $sleeptime
tweettoken=$(echo "$composepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
update=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "")

logoutpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "")

echo "[+] Logging out..." && sleep $sleeptime
logouttoken=$(echo "$logoutpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
logout=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$logouttoken" "")

rm "cookie.txt"


Share this post

One thought on “Send tweets in command-line with Bash

  1. I tried and it didn’t work…
    Is there any reason why? Is it that the interface now is different? Or is it that I need to install something called tweet dev? Any grip please let me know. Thanks!!

Post Comment