I need to URL encode a string using a shell function that will run in BusyBox Ash, Dash, Bash and ZSH.
It will run in different Docker containers, so it'd be good to have as little dependencies to install as possible.
Notes:
The question "URL encoding a string in bash script" asks for a Bash-specific script, and the only provided answer depends on PHP being installed on the container.
The question "How to urlencode data for curl command?" is specific to curl, but is certainly open to non-specific answers. However, none of the 25 answers seem to apply. One of the answers there only works to send data to curl, while some are specific to bash or ksh, some others require Perl, PHP, Python, Lua, NodeJS, Ruby, gridsite-clients, uni2ascii, jq, awk or sed to be installed. One of them doesn't require additional dependencies, but doesn't preserve characters like
a
,1
and~
.
What I'd expect to have:
$> urlencode '/'
%2f
$> urlencode 'ç'
%c3%a7
$> urlencode '*'
%2a
$> urlencode abc-123~6
abc-123~6
$> urlencode 'a test ?*ç '
a%20test%20%3f%2a%c3%a7%20