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

后端 未结 3 493
梦毁少年i
梦毁少年i 2021-02-05 01:11

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 02:00

    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.

提交回复
热议问题