Regular Expressions and Quoting
Regular expressions are different from bash wildcard expansion in that they are far more complete and clearly defined. Entire books can be (and have been) written on regular expressions. They are not directly related to the shell because other than bash’s =~ syntax, only external tools such as grep, sed, and awk actually use regular expressions. Because shell expansion and regular expressions use very similar syntax, a regular expression passed from the shell to an external command could be parsed by the shell on the way; various quoting techniques can be deployed to avoid this problem.
Overview of Regular Expressions
Regular Expressions are interpreted by a command such as sed to achieve some quite powerful results. While these commands are used in various recipes in this book, the actual usage of these is beyond the scope of this book. http://sed.sourceforge.net/sed1line.txt is one excellent list of one-line sed recipes; there are many online tutorials and books available that cover these commands in great detail. Here the details will be dealt with briefly in order to discuss how to use them with the shell.
$ cat myfile foo="hello is bonjour" bar="goodbye is aureviour" foo1=$foo bar1=$bar $ foo=bonjour $ bar=aurevoir $ sed s/$foo/$bar/g myfile foo="hello is aurevoir" bar="goodbye is aureviour" foo1=$foo bar1=$bar $ sed s/"$foo"/"$bar"/g myfile foo="hello is aurevoir" bar="goodbye is aureviour" foo1=$foo bar1=$bar $
This simple example is passed ...
Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.