Friday, June 10, 2011

Shorten a URL on using CURL

I often have to shorten URLs when I'm tweeting or simply sending a URL to a friend via email or on instant messenger. Since I generally have a terminal window open I figured it would be expeditious to come up with a shell script to do this for me.

The first thing you have to do is sign up for an account on if you haven't already, next you have to obtain an API key. You can sign up for a account at: if you already have an account you can get your API key at:

First create a shell script called


curl -s "$1&format=txt" | pbcopy

This script uses curl in silent mode to connect to the API and shorten the url specified by longUrl. Since we want to shorten a variety of URLs I've used $1, which refers to the first argument the script was invoked with. You'll also notice the format parameter, this can be set to xml, json or txt, we're using txt so we don't have to do any parsing. We are piping the output of our curl command into pbcopy a command line utility provided by OS X which provides copying to the clipboard. Pbcopy basically copies all output it receives into the clipboard.

Make sure to fill in YOURLOGIN with your login and YOURAPIKEY with your API key. Save the shell script somewhere it's not going to be deleted by accident or moved. If you use bash open your .bash_profile file in your home directory. You may have to create it if you haven't made any changes to it before. Add the following line:

alias shorten='/bin/bash /PATHTOSCRIPT/'

Where PATHTOSCRIPT is the path to where you have stored your script. This alias will allow us to invoke our script from anywhere using "shorten" instead of the full path to the script. In order for your .bash_profile to be reloaded and the alias to become active, you need to either type source .bash_profile or open a new terminal window.

Now when you type:

shorten YOURURL

Where YOURURL is any URL you wish to shorten, the shortened version will be copied directly into your machine's clipboard.


  1. effin awesome.

    this comment uncrowded by many not-quite-on-topic questions (motivated by personal projects, whose daydreams are often set bombastic soundtracks, & references aloud often end with "& then all will bow down before me,") to save its author (me) the embarrassing realization of just how silly they in fact might very well seem. that would hurt my, uh, score.

    the above metaphor nonetheless allows information to be disclosed on the suspicion that you know exactly what i mean.

    thanks! you made my day -- rereading man pages does not always work, perhaps especially when one starts out only knowing the nothing sandbox that corporate eulas drool to keep invisibly locked.... and besides, it was not so much the commands as the strategies --

    be seeing you...

  2. Great stuff! Just added my own domain to bitly and this will make it way more simpler to shorten URLs.

  3. Cool. I could extend it a little bit :) Instead of using $1, use pbpaste. It will paste the original url into the script. One step less :) Heres the code:

    curl -s "$url&format=txt" | pbcopy

    Additional, if you put this into an automator app, you can access es via spotlight or Alfred & Co.

  4. But, what one major problem is faced by most of the people is the long URL. They not only like to ignore the long URL but also find out the ways to shorten them. url shortener

  5. Wow what a Great Information about World Day its very nice informative post. thanks for the post.

  6. Maybe their most concerning issue is that is doesn't resemble a URL by any means. Except if you're acquainted with running from monkeys in the Mideast. tinyurl

  7. It is safe to say that you are utilizing a link shortener yet for your site links and offshoot links? If not, you ought to be!bitly

  8. così come è non potremmo mai sapere in quanti lo hanno scaricato. Dobbiamo quindi andare su e incollarlo nel campo apposito quindi andare su Shorten URL. Si otterrà questo indirizzo accorciato url shortener

  9. Very useful information and thanks for sharing. Donald’S Office Mod Apk