Bash: Argument Parsing
Drew Stokes
595

Nice one. Elegant in its simplicity.

I’m pretty sure you could handle the --long-option=value form as well without changing the case statement, if you add in the following line right before case:

[[ $1 == --*=* ]] && set -- "${1%%=*}" "${1#*=}" "${@:2}"

This just splits the argument into two on the equals sign before case sees it.

Also note that when you join parameters into PARAMS and then try to evaluate them back into the positional parameters, you will split any parameters which contained whitespace, which results in a different parameter list than you were given. Unfortunately I don’t think you can preserve such parameters without either changing the IFS variable or using an array to hold them instead. I’d go with an array.

Like what you read? Give Ted Lilley a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.