urlencode and urldecode for bash scripting using sed

19. June 2011

To urlencode or -decode strings in bash scripts, one can use simple sed scripts containing one rule for every character. You can download the urlencode.sed and urldecode.sed files.

url="http://gimi.name/snippets/urlencode-and-urldecode-for-bash-scripting-using-sed?randomOption=foo,bar,baz"
encodedUrl=$(echo "$url" | sed -f urlencode.sed)
url=$(echo "$encodedUrl" | sed -f urldecode.sed)
 
echo "encoded: $encodedUrl"
echo "decoded: $url"

Result:

encoded: http%3a%2f%2fgimi%2ename%2fsnippets%2furlencode%2dand%2durldecode%2dfor%2dbash%2dscripting%2dusing%2dsed%3frandomOption=foo%2cbar%2cbaz
decoded: http://gimi.name/snippets/urlencode-and-urldecode-for-bash-scripting-using-sed?randomOption=foo,bar,baz

3 comments zu 'urlencode and urldecode for bash scripting using sed'

  1. DonielS said the 18. April 2012 at 15:28:

    Thanks! This saved a lot of time.

    I added handling for th + sign to the end of my urldecode.sed file:

    s/+/ /gi

  2. FileNotFound said the 31. October 2012 at 18:26:

    File Not Found

  3. gimi said the 5. December 2012 at 22:07:

    Oh noes. Links are fixed now :)

Write a comment

Based on a theme by BenediktRB • Top left picture from piqs.de, cc-by • Powered by WordpressRSS Feed