Skip to Content
Learn Linux Shell Scripting - Fundamentals of Bash 4.4
book

Learn Linux Shell Scripting - Fundamentals of Bash 4.4

by Sebastiaan Tammer
December 2018
Beginner
452 pages
12h 17m
English
Packt Publishing
Content preview from Learn Linux Shell Scripting - Fundamentals of Bash 4.4

Pattern substitution

Simply said, pattern substitution allows us to substitute a pattern with something else (who would have thought!). This is what we could already do with sed:

reader@ubuntu:~/scripts/chapter_16$ echo "Hi"Hireader@ubuntu:~/scripts/chapter_16$ echo "Hi" | sed 's/Hi/Bye/'Bye

Initially, our echo contains the word Hi. We then pipe it through sed, in which we look for the pattern Hi, which we will substitute with Bye. The s at the front of the instruction to sed signals that we're searching and replacing.

Behold, after sed is done parsing the stream, we end up with Bye on our screen.

If we want to do the same when using a variable, we have two options: we'll either parse it through sed as we did previously, or we'll turn to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Bash Scripting Fundamentals

Bash Scripting Fundamentals

Sander van Vugt

Publisher Resources

ISBN: 9781788995597Supplemental Content