Last active
January 27, 2023 01:32
-
-
Save pablochacin/8016575 to your computer and use it in GitHub Desktop.
A one liner to parse a http query string in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# the following one liner creates a shell variable from every parameter in a | |
# the query string in the variable QUERY, of the form p1=v1&p2=v2,... and sets it to | |
# the corresponding value so that parameters can be accessed by its name $p1, $p2, ... | |
# | |
for p in ${QUERY//&/ };do kvp=( ${p/=/ } ); k=${kvp[0]};v=${kvp[1]};eval $k=$v;done |
If you are just going to assume that nothing is URL encoded and use eval to set variables, then all you really need is:
eval "${QUERY_STRING//&/$'\n'}"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try this
protect_shell(){
}