Skip to content

Instantly share code, notes, and snippets.

@darshitpp
Created April 1, 2021 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darshitpp/1b5e1ff63934aeeb170b4e215c147507 to your computer and use it in GitHub Desktop.
Save darshitpp/1b5e1ff63934aeeb170b4e215c147507 to your computer and use it in GitHub Desktop.
URL Encode/Decode from Bash/ZSH using Python 2.7 and Python 3
# Add the following in your ~/.bashrc or ~/.zshrc
# No need to visit urlencoder.org or urldecoder.org!!
# Python 2.7
# >url_encode 'Hello, this is me'
# >=====
# Hello%2C%20this%20is%20me
url_encode() {
python -c "import urllib; print '=====\n' + urllib.quote_plus('$1')"
}
# >url_encode 'Hello%2C%20this%20is%20me'
# >=====
# Hello, this is me
url_decode() {
python -c "import urllib; print '=====\n' + urllib.unquote_plus('$1')"
}
# Python 3, courtesy https://twitter.com/arion_miles
# >url_encode 'Hello, this is me'
# >=====
# Hello%2C%20this%20is%20me
url_encode() {
python3 -c "from urllib.parse import quote_plus; print('='*5, quote_plus('$1'), sep='\n')"
}
# >url_encode 'Hello%2C%20this%20is%20me'
# >=====
# Hello, this is me
url_decode() {
python3 -c "from urllib.parse import unquote_plus; print('='*5, unquote_plus('$1'), sep='\n')"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment