How do i use double square brackets in bash shell scripts?



  • Also see: How do i use double quotes in a bash shell script within single square brackets?

    Following on from the article above.

    Note: Something to take into consideration is the use of double square brackets for testing conditions and use of double quotes within them is different.

    Within double square brackets, the double quotes with them are optional.

    The use of double square brackets brings in the use of pattern matching and regular expressions.

    For example, to match any combination of response yes uppercase or lowercase, in the script below, we can use pattern matching.

    $ cat doublebrackets.sh
    echo "Do you like to apples? "
    read ans
    if [[ $ans =  [Yy][Ee][Ss] ]]; then
       echo "That's great you're eating healthy!"
    fi
    

    Running the script.

    $ ./doublebrackets.sh
    Do you like to apples? 
    yES
    That's great you're eating healthy!
    

    You can use any combination of uppercase or lowercase letters of the word yes.

    Generally, you would use double square brackets in shell scripts than single square brackets.


Log in to reply
 

© Lightnetics 2024