Wednesday, July 15, 2009

Shell: Aliases for Escaping/Unescaping Query Parameters

Here are a couple more Unix aliases, this time for quickly escaping and unescaping URL query parameters. These are defined in my ~/.bash_aliases file.

alias urlencode="perl -MURI::Escape -ple '\$_=uri_escape(\$_); s/%20/+/g'"
alias urldecode="perl -MURI::Escape -ple '\$_=uri_unescape(\$_); s/\+/ /'"
To escape/encode all unsafe characters in a query parameter:

$ echo "blue socks?" | urlencode
And to unescape/decode a parameter:

$ echo blue+socks%3F | urldecode
blue socks?
These aliases depend on Perl and the URI::Escape module which are standard on most modern Unixes.

No comments: