How do I use regular expressions in bash scripts ?

How do I use regular expressions in bash scripts ?

Asked on January 11, 2019 in Regex.
Add Comment


  • 4 Answer(s)

    Use regular expressions in bash scripts It changed between bash 3.1 and  bash 3.2:

    Terse description of the new features added to bash-3.2 as the release of bash-3.1.

    Quoting the string argument to the [[ command’s =~ operator now forces string matching, as with the other pattern-matching operators.

    Use below code for without the quotes:

    i="test"
    if [[ $i =~ 200[78] ]] ; then
      echo "OK"
    else
      echo "not OK"
    fi
    
    Answered on January 11, 2019.
    Add Comment

    By using spaces around the operator =~:

    i="test"
    if [[ $i =~ "200[78]" ]];
    then
      echo "OK"
    else
      echo "not OK"
    fi
    

     

    Answered on January 11, 2019.
    Add Comment

    Regular expressions or regex or regexp are basically strings of character that define a search pattern, they can be used for performing ‘Search’ or ‘Search & Replace’ operations as well as can be used to validate a condition like password policy etc.

    Answered on May 20, 2019.
    Add Comment

    Regex is a very powerful tool that is available at our disposal & best thing about using regex is that they can be used in almost every computer language. So if you are Bash Scripting or creating a Python program, we can use regex or we can also write a single line search query.

    Answered on May 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.