Skip to content

Instantly share code, notes, and snippets.

@hazcod
Last active January 24, 2020 19:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hazcod/da9ec610c3d50ebff7dd5e7cac76de05 to your computer and use it in GitHub Desktop.
Save hazcod/da9ec610c3d50ebff7dd5e7cac76de05 to your computer and use it in GitHub Desktop.
UrlEncode function in Bash
# put this in your .bashrc or .bash_profile
function urlencode(){
[ -z "$1" ] || echo -n "$@" | hexdump -v -e '/1 "%02x"' | sed 's/\(..\)/%\1/g'
}
@yrro
Copy link

yrro commented Feb 27, 2017

If you only have coreutils lying around: od -v -A n -t x1 | tr ' ' %

@hazcod
Copy link
Author

hazcod commented Feb 27, 2017

@yrro: Thanks, but this seems to give me something weird on macos:

$ echo -n x | od -v -A n -t x1 | tr ' ' %
%%%%%%%%%%%78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment