URL Encode Text In Bash

In an upcoming Byte I’ll be sharing a script that I built which opens pull request URLs based on Git branch names. However to do this correctly, we need to URL encode the input data. With the help of jq and 5 lines of code, we can URL encode any text we need.

/usr/local/bin/url-encode
#!/bin/bash

while IFS= read -r line; do
	echo "$line" | jq -Rr @uri
done

To use this, we can call it like so:

echo "Author: Mark Skelton" | url-encode

Which should produce this as the result:

Author%3A%20Mark%20Skelton