Search code examples
bashgetopts

Ensuring a single colon at start of string


I am using getopts to parse options. I want to ensure that the optstring has only a single colon at the start, and if this does not exist I want to introduce it.

Here is an example of the initialisation of optstring

  local optstring="sn:z:" 

Solution

  • optstring=:${optstring#:}
    

    First we unconditionally add a colon, then we use a parameter expansion to expand our string with any preexisting colon removed.