bash: Test if a string has nonzero length.



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

    Using the -n test option.

    echo -n "Type in your string here:"
    read MyVar
    
    if [[ -n "$MyVar" ]];then
       echo "MyVar is not an empty string."
    else
       echo "MyVar is an empty string."
    fi
    

    Enter a string of characters.

    $ ./test.sh
    Type in your string, here:hello
    MyVar is not an empty string.
    

    Enter a blank string.

    $ ./test.sh
    Type in your string, here:
    MyVar is an empty string.
    

Log in to reply
 

© Lightnetics 2024