Linux

Parse query string in shell script

BASH Shell

Sometimes we have to integrate multiple languages like node.js/php/shell script into bash shell script and parse query string to array for supplying query string data for manipulation.

Here is the simple script which will help you to achieve that goal:

#!/usr/bin/env bash
declare -A querydict
populate_querystring_array () {
query=”$1″
while IFS== read arg value
do
querydict[“$arg”]=”$value”
done < <(echo “$query” | sed ‘s/&/\n/g’ )
}

q=’user=mindgeek&password=1234f’
populate_querystring_array “$q”
printf “${querydict[user]}\n”

You Might Also Like