Skip to content

Instantly share code, notes, and snippets.

@pablochacin
Last active January 27, 2023 01:32
Show Gist options
  • Save pablochacin/8016575 to your computer and use it in GitHub Desktop.
Save pablochacin/8016575 to your computer and use it in GitHub Desktop.
A one liner to parse a http query string in bash
#
# 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
@jasonk
Copy link

jasonk commented Jan 27, 2023

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