Expression grouping

You can use parentheses () to group characters or words to make them one piece in the eyes of the regex engine:

$ echo "welcome to shell scripting" | awk '/(shell scripting)/{print $0}'
$ echo "welcome to bash scripting" | awk '/(shell scripting)/{print $0}'
$ echo "welcome to shell scripting" | sed -r -n '/(shell scripting)/p'
$ echo "welcome to bash scripting" | sed -r -n '/(shell scripting)/p'

Since the shell scripting string is grouped with parentheses, it will be treated as a single piece.

So, if the entire sentence doesn't exist, the pattern will fail.

You may have realized that you can achieve that without parentheses like this:

$ echo "welcome to shell scripting" | sed -r -n '/shell scripting/p'

So, what is the benefit of using parentheses or expression grouping? Check the following examples to know the difference.

You can use any of the ERE characters with the grouping parentheses:

$ echo "welcome to shell scripting" | awk '/(bash scripting)?/{print $0}'
$ echo "welcome to shell scripting" | awk '/(bash scripting)+/{print $0}'
$ echo "welcome to shell scripting" | sed -r -n '/(bash scripting)?/p'
$ echo "welcome to shell scripting" | sed -r -n '/(bash scripting)+/p'

In the first example, we search for the whole sentence bash scripting for zero or one time using the question mark, and because the whole sentence doesn't exist, the pattern succeeds.

Without expression grouping, you won't get the same result.