可选参数值

函数

#${1:-text} gives text when the argument is not given or empty
sayGoodbye() {
    local message=${1:-"Goodbye!"}
    local times=${2:-1}
    local i
    for ((i = 0; i < times; i++)); do
        echo "$message"
    done
}

sayGoodbye
#prints "Goodbye!"

sayGoodbye "see you"
#prints "see you"

sayGoodbye "bye" 2
#prints "bye" twice

#the arguments have no names: "" skips the first one
sayGoodbye "" 2
#prints "Goodbye!" twice