Simple bash argument parser
Don't go against the language


  The main arguments can be thought of as being a single argument, but I do not want to force users to quote the entire thing, so when the argument contains spaces I must be able to handle multiple arguments.


If you want to a value containing spaces as a single argument,
then you and your users should double-quote it,
otherwise Bash will perform word splitting.
This is a fundamental principle in Bash,
and it's better to play along with it than to go against.

Trying to go against will get you into all kinds of trouble.
For example, what would you expect for?

my_command this      is   a test -new -- sed "s|a|b|"


That is, multiple spaces between words.
Those spaces will be lost,
the script will behave the same way as if there was a single space in between.

Keep in mind that users will have to quote special characters anyway.
You cannot shelter them from quoting.
It's better to learn the basic rules of word splitting and quoting early,
rather than trying to work around it with hacky solutions.

Assign arrays to arrays

This statement assigns an array to a non-array:


ext_command="${@:count}"



This way you lose the ability to expand the original value correctly quoted.

Take for example this input:


my_command test -new -- sed "s|a| |"



Notice the space in the sed pattern.

And let's say the script uses ext_command like this:

ls | "$ext_command"


This will not work as intended (replacing "a" with spaces), because the original arguments are not preserved correctly.

Using an array you could leave this option open, that is:

ext_command=("${@:count}")


And then later:

ls | "${ext_command[@]}"


Minor points

Instead of this:


local inp=""



You can write simply:

local inp




Instead of this:


count=$((count+1))



You can write simply:

((count++))




Instead of this:


if [[ "$i" == '--' ]]
then
    ext_command="${@:count}"
    break
else
    # a long block of code ...
fi



It's more readable like this:

if [[ "$i" == '--' ]]
then
    ext_command="${@:count}"
    break
fi

# a long block of code ... but less deeply nested