Ubuntu – How to encode and decode percent-encoded strings on the command line

command lineconversionencodingtext;url

How can I encode and decode percent-encoded (URL encoded) strings on the command line?

I'm looking for a solution that can do this:

$ percent-encode "ændrük"
%C3%A6ndr%C3%BCk
$ percent-decode "%C3%A6ndr%C3%BCk"
ændrük

Best Answer

These commands do what you want:

python -c "import urllib, sys; print urllib.quote(sys.argv[1])" æ
python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" %C3%A6

If you want to encode spaces as +, replace urllib.quote with urllib.quote_plus.

I'm guessing you will want to alias them ;-)