How to conditionally add flags to shell scripts?

后端 未结 3 1737
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-19 08:14

I want to run a command inside my bash script with or without a -v flag depending on if the environment variable $VERBOSE is defined. Something like this:



        
3条回答
  •  忘掉有多难
    2021-02-19 08:57

    For readability, consider using an array to store the arguments.

    args=(
      -m "$MONGO_HOST"
      -t "$NEO_URI"
      -stdout
    )
    if [[ -v VERBOSE ]]; then
        args+=(-v)
    fi
    /usr/local/bin/mongo-connector "${args[@]}"
    

提交回复
热议问题