bash getopts multiple arguments or default value bash getopts multiple arguments or default value bash bash

bash getopts multiple arguments or default value


You can just provide default value before while loop:

directory=mydirdepth=123while getopts "hd:l:" opt; do    case $opt in        d ) directory=$OPTARG;;        l ) depth=$OPTARG;;        h ) usage        exit 0;;        *) usage        exit 1;;    esacdoneecho "<$directory> <$depth>"


After your getopts, try this

if [ -z "$directory" ]; then directory="directory"; fi

-z means if the variable $directory is null or empty. Then if user doesnt enter an argument for -d, then the script will default to directory="/whatever/files/"

At least if I understand your question correctly, this should give you a default value for -d if a value is not entered.