InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

bash – how to use regex in if condition

By admin on Jan 1, 2016

Bash built in double square brackets can be used for regex match in if condition. This can be pretty powerful and can be used in writing complex regex tests. Here are some examples.
bash-regex

Bash regex match

Here is a simple example to check if a url begins with /foo after the host part. Default match is case sensitive.

#!/bin/bash
URL=http://somedomain.com/foo/bar
if [[ $URL =~ ^http://somedomain.com/foo.*$ ]]; then
  echo "matched"
fi
if [[ $URL =~ ^http://somedomain.com/foo ]]; then
  echo "matched again"
fi
matched
matched again
Env: GNU bash, version 4.2.46

Bash regex match – case insensitive

To do case insensitive match set nocasematch using

shopt -s nocasematch

To unset nocasematch

shopt -u nocasematch

Here is sample code with case insensitive regex match

#!/bin/bash
shopt -s nocasematch
URL=http://SOMEDOMAIN.COM/foo/bar
if [[ $URL =~ ^http://somedomain.com/foo.*$ ]]; then
  echo "matched"
fi
matched
Env: GNU bash, version 4.2.46

Bash regex match – capture matched pattern

Print the matched content using ${BASH_REMATCH[n]}

#!/bin/bash
URL=http://somedomain.com/foo/bar
if [[ $URL =~ ^http://somedomain.com(/foo.*)$ ]]; then
  echo "matched"
  echo "full match: ${BASH_REMATCH[0]}"
  echo "first match: ${BASH_REMATCH[1]}"
fi
matched
full match: http://somedomain.com/foo/bar
first match: /foo/bar
Env: GNU bash, version 4.2.46

Suggested posts:

  1. Bash – how to compare file timestamps
  2. How to specify environment variable for a command on Linux
  3. Can global variables be modified in bash function?
  4. Bash – newline and other escape character in string
  5. PHP – convert dos newline to unix format
  6. Python map examples
  7. python print examples
  8. Bash – local and global variables
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress