ENCODED=$(echo -n "value to encode" | \ perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg'); echo $ENCODED
This command line percent-encodes strings, even binary data. You can use redirection. I used
`echo -n` because I didn’t want a newline (
%0A) tagged onto the end. I’ll be using this in a bash script to send HTTP POST data via curl.
I don’t know if this depends on Perl multibyte settings or anything else. I might have just been lucky. Perl isn’t my thing.