Parameter expansion in Bash deals with more than just default values, input checking, and parameter length. It actually also allows us to manipulate the variables before we use them. In this second part of this chapter, we'll explore the capabilities within parameter expansion that deal with variable manipulation (our terminology; as far as Bash is concerned, these are just normal parameter expansions).
We'll kick this off with pattern substitution, something you should be familiar with after our explanation of sed
in Chapter 10, Regular Expressions.
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"
Hi
reader@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 patternHi
, which we will substitute...