Urlencode and urldecode from a command line

by Ruslan Spivak on June 2, 2010

Recently I have worked with a REST Web service and had to URL encode a lot of values from the command line.

If all you need is to URL encode data for a POST request you can use curl with –data-urlencode parameter, but I needed to encode values that went into a URL query string.
So for the task I wrote a short code in Python to do just that (there are examples available in BASH or Perl, but this is a pure Python using only standard library):

python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"

Assign it to an alias by putting single quotes around the expression and you’re good to go:

$ alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'


$ urlencode 'q werty=/;'

And the decode part:

python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"
$ alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
$ urldecode 'q+werty%3D%2F%3B'
q werty=/;
If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

{ 9 comments… read them below or add one }

Gabriel October 12, 2011 at 12:43 PM

You rock. Exactly what I was looking for. Thank you!


Ruslan Spivak October 12, 2011 at 8:27 PM

You’re welcome :)


Gregory Rasdi January 20, 2012 at 7:45 AM

Hi Ruslan, lovely work I love this website great stuff!


Peter Jenkins February 21, 2012 at 2:44 PM

Handy. Replace “sys.argv[1]” with “sys.stdin.read()” to use in a pipeline.


Erik Lundmark November 14, 2012 at 4:18 PM

If you want the node.js version of this:
alias urlencode_js='node -e "console.log(encodeURIComponent(process.argv[1]))"'
alias urldecode_js='node -e "console.log(decodeURIComponent(process.argv[1]))"'


url encode June 22, 2013 at 11:38 AM

great tutorial. thanks


Keith Kim June 26, 2013 at 3:15 PM

Hey Ruslan, what a coincidence my finding your blog!

Nice one. I needed to do just this, but with binary data and found I could do this to omit final newline:

python -c “from __future__ import print_function; import sys, urllib as ul; print(ul.unquote_plus(sys.argv[1]), end=”)” ‘%0a%02%08%01%18%01%22%04%0a%02%08%02′ >data.bin


Ruslan Spivak June 28, 2013 at 6:43 PM

Cool :)


Aleksei November 26, 2013 at 10:01 AM

Sometimes one needs to make ALL url-encoded filenames as decoded – below – my solution:

first file – urldecode.sh (make sure it is executable – chmod +x urldecode.sh)
##start content
python -c “import sys, urllib as ul; print ul.unquote_plus(sys.stdin.read())”
##end content

second file – urldecoder.sh(make sure it is executable)
##start content

function readfile()
while read line
done > encode.txt
find $1 -name “*.*” | xargs ls | “$urldecode” >> norm.txt

readfile “encode.txt”
readfile “norm.txt”


for i in “${encodeArr[@]}”
mv -n “$i” “${normArr[k]}”
##end content

how to use:
./urldecoder.sh path-to-folder-containing-files-with-encoded-filenames

it may produce messages like:
mv: `../Desktop/ticket/993/screen.png’ and `../Desktop/ticket/993/screen.png’ are the same file
but it is no matter – cosider it as a warn



Speak your mind

Previous post:

Next post: