I want to use multiple instances of command line paramater such as the -d option used by PHP for passing PHP options. I am currently using the getopts command in bash.With PHP invocation it would look like this:php -f aPHPscript.php -d memory_limit=120M -d apc=1 -d max_execution_time=120How would the multiple instances of the parameter be extracted in a bash script? Is there a bash command for doing that?
What is the bash syntax for extracting the values from multiple instances of the same argument?
bash;command line;parameter
Bash doesn't typically care about the values of the arguments, it's more their ordering, and how they're separated on the command line, by default parsing them based on a space between each argument.You can see this with a simple for loop construct in a shell script like so:#!/bin/bashecho echo ARGS: $@echo echo parsed args:for i in $@; do    echo $idoneExample$ ./parse.bash -f aPHPscript.php -d memory_limit=120M -d apc=1 \     -d max_execution_time=120ARGS: -f aPHPscript.php -d memory_limit=120M -d apc=1 -d max_execution_time=120parsed args:-faPHPscript.php-dmemory_limit=120M-dapc=1-dmax_execution_time=120In the above, each iteration through the loop is peeling off the next argument that was passed into the script parse.bash. So rather then use getopts you could always do something like this.Using while + caseI typically don't use getopts and do things as you're inquiring about like so with a while loop using a case statement to parse the arguments as required.$ more parse2.bash #!/bin/bashwhile [[ $# > 1 ]]dokey=$1shift#  -f aPHPscript.php -d memory_limit=120M -d apc=1 #+ -d max_execution_time=120case $key in    -f)    f_ARG=$1    shift    ;;    -d)    d_ARGS=( ${d_ARGS[@]} $1 )    shift    ;;    *)            # unknown option    ;;esacdoneecho $f_ARGecho ${d_ARGS[@]}When we run it with your arguments we can see that it was able to parse your -d arguments into an array, $d_ARGS.$ ./parse2.bash -f aPHPscript.php -d memory_limit=120M -d apc=1 \      -d max_execution_time=120aPHPscript.phpmemory_limit=120M apc=1 max_execution_time=120ReferencesHow do I parse command line arguments in bash?Chapter 27. Arrays