I wanted a script to make it quick to generate consistent templates for this repo, so I wanted to take the subject (in this case Bash pattern substitution), and replace the spaces with underscores.
We can replace underscores in a string with straight up Bash parameter substitution.
variable=${variable// /_}
variable
is the variable name, I think that’s obvious.//
is the command to replace every occurance of the pattern in the string (a single slash [/
] will just do the first occurance)_
is the pattern to replace it with… I’m replacing it with underscores. Omitting the replacement would just delete the pattern from the string.A generalized form of this would be:
variable=${variable//*PATTERN*/*REPLACEMENT*}
Wednesday, February 19th, 2025
2025-02-19