How can I encode and decode percent-encoded (URL encoded) strings on the command line?
I'm looking for a solution that can do this:
$ percent-encode "ændrük" %C3%A6ndr%C3%BCk $ percent-decode "%C3%A6ndr%C3%BCk" ændrük
These commands do what you want:
If you want to encode spaces as
I'm guessing you will want to alias them ;-)
I found a package,
Unfortunately, it does not decode stdin or a command line option, but only rename a file, so you have to create a dummy file to obtain the decoding (the name of the renamed file), but with some bash scripting the process can be automated.
No information about the encoding part, even because it could be questionable which characters to encode. Only non-ASCII?
I think there should be some better tool/method.
Try the following command line:
You may define it as alias and add it to your shell rc files:
Then every time when you need it, simply go with:
When scripting, you can use the following syntax:
However above syntax won't handle pluses (
You can also use the following
bash + xxd
Bash function with
Try to define the following aliases:
Using PHP you can try the following command:
In Perl you can use
Or to process a file:
Try anon solution:
decoding file names
If you need to remove url encoding from the file names, use
Pure bash solution for decoding only:
I can't comment on best answer in this thread, so here is mine.
Personally, I use these aliases for URL encoding and decoding:
Both commands allow you to convert data, passed as a command line argument or read it from standard input, because both one-liners check whether there are command line arguments (even empty ones) and process them or just read standard input otherwise.
... according to @muru comment.
Percent-encode reserved URI characters and non-ASCII characters
Percent-encode all characters
Percent-encode all characters except ASCII alphanumeric characters in Bash
Here is a POSIX awk function for encoding: