How do I print a message if a variable is not set and then exit the bash shell script?



  • Note: Do not use for interactive shell scripts.

    Using the syntax ${name?word}

    URL variable is not set.

    WEBSITE_URL=${URL?"URL variable is not set"}
    echo $WEBSITE_URL
    echo $URL
    
    $ ./varsubs.sh
    ./varsubs.sh: line 2: URL: URL variable is not set
    

    When the URL variable is set.

    URL=www.gnu.org
    WEBSITE_URL=${URL?"URL variable is not set"}
    echo $WEBSITE_URL
    echo $URL
    
    $ ./varsubs.sh
    www.gnu.org
    www.gnu.org
    

Log in to reply
 

© Lightnetics 2024