Bash urlencode and urldecode (original) (raw)
Star (407) You must be signed in to star a gist
Fork (73) You must be signed in to fork a gist
Select an option
Clone this repository at <script src="https://gist.github.com/cdown/1163649.js"></script>
- Save cdown/1163649 to your computer and use it in GitHub Desktop.
Select an option
Clone this repository at <script src="https://gist.github.com/cdown/1163649.js"></script>
Save cdown/1163649 to your computer and use it in GitHub Desktop.
Bash urlencode and urldecode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
| urlencode() { |
|---|
| # urlencode |
| old_lc_collate=$LC_COLLATE |
| LC_COLLATE=C |
| local length="${#1}" |
| for (( i = 0; i < length; i++ )); do |
| local c="${1:$i:1}" |
| case $c in |
| [a-zA-Z0-9.~_-]) printf '%s' "$c" ;; |
| *) printf '%%%02X' "'$c" ;; |
| esac |
| done |
| LC_COLLATE=$old_lc_collate |
| } |
| urldecode() { |
| # urldecode |
| local url_encoded="${1//+/ }" |
| printf '%b' "${url_encoded//%/\\x}" |
| } |