Correct usage of bash getopts using long options

bash, getopts

Solution

I found that the code in question is for `ksh` and not `bash`. For `getopts` we can't use long options. I ended up manually parsing arguments as below

while test -n "$1"; do
    case "$1" in
      -c|--mode)
          mode=$2
          shift 2
          ;;  
      -d|--file1)
          file1=$2
          shift 2
          ;;  
      -e|--file2)
          file2=$2
          shift 2
          ;;  
    esac
done 

Problem

I have written below code for using long options with `getopts`, but it doesn't work (arguments have no effect on values of the variables). What is the correct syntax? ``` while getopts "c:(mode)d:(file1)e:(file2)" opt; do case $opt in -c|--mode) mode=$OPTARG ;; -d|--file1) file1=$OPTARG ;; -e|--file2) file2=$OPTARG ;; esac done ```

Original source

Related problems