Url encode: Difference between revisions
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
==URL encoding function== | ==URL encoding function== | ||
* Simple way to encode URLs for bash scripts.. Brainless but seems to work ok for most use cases.. | * Simple way to encode URLs for bash scripts.. Brainless but seems to work ok for most use cases.. | ||
* Yes, this will encode the '/' character, but curl does not spit errors and I have not seen a failure doing things this way | * Yes, this will encode the '/' as the '%2F' character, but curl does not spit errors and I have not seen a failure doing things this way | ||
<pre> | <pre> |
Latest revision as of 10:29, 10 July 2025
URL encoding function
- Simple way to encode URLs for bash scripts.. Brainless but seems to work ok for most use cases..
- Yes, this will encode the '/' as the '%2F' character, but curl does not spit errors and I have not seen a failure doing things this way
urlencode() { local string="$1" local encoded="" local pos c o for (( pos=0 ; pos<${#string} ; pos++ )); do c=${string:$pos:1} case "$c" in [a-zA-Z0-9.~_-]) o="$c" ;; *) printf -v o '%%%02X' "'$c" esac encoded+="$o" done echo "$encoded" }