bash: Variable substitution - If variable is set use it else print word and exit.



  • Take into account the main readme for this section. README first.

    Syntax is:
    ${name?word}

    Use the variable $name is set, otherwise print word and exit.

    In a shell script.

    Brazil1=SugarCane
    Amazon=${Brazil1?NoDeforestation}
    echo "Healthy Planet"
    

    With Brazil1 set to SugarCane, run the script.

    $ ./healthyplanet
    Healthy Planet
    

    Now comment out Brazil1 from the script and run it again. The script exited before the echo statement in the script, because Brazil1 was not set.

    $ ./healthplanet
    ./healthplanet: line 4: Brazil1: NoDeforestation
    $ echo $?
    1
    

Log in to reply
 

© Lightnetics 2024