Bash: How to set a variable from argument, and with a default value

后端 未结 3 1380
耶瑟儿~
耶瑟儿~ 2021-02-05 01:17

It is pretty clear that with shell scripting this sort of thing can be accomplished in a huge number of ways (more than most programming languages) because of all the different

3条回答
  •  孤城傲影
    2021-02-05 01:58

    How about this:

    DIR=.
    if [ $# -gt 0 ]; then
      DIR=$1
    fi
    

    $# is the number of arguments given to the script, and -gt means "greater than", so you basically set DIR to the default value, and if the user has specified an argument, then you set DIR to that instead.

提交回复
热议问题