Override a variable in a Bash script from the command line

前端 未结 4 591
悲&欢浪女
悲&欢浪女 2020-12-13 00:19

How do you override a variable in your Bash script from the command line?

I know how to pass variables in, but I just want something like ./myscript.sh -Dvar=v

4条回答
  •  心在旅途
    2020-12-13 00:50

    You should specify the variable with the following syntax:

    MYVAR=74 ./myscript.sh
    

    Within the script, check if it is already set before setting it:

    if [ ! -z $MYVAR ]; then
        #do something
    fi
    

提交回复
热议问题