Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pattern substitution
$ VAR='XOXOXOX'
$ echo ${VAR/X/.} ## first occurence only (anywhere)
.OXOXOX
$ echo ${VAR//X/.} ## all occurences
.O.O.O.
$ VAR='OXOXOX'
$ echo ${VAR/#X/.} ## first occurence (only if beginning match)
OXOXOX
$ VAR='XOXOXO'
$ echo ${VAR/$X/.} ## first occurence (only if ending match)
XOXOXO
## Real file example:
# printf-style value substituition
$ file='/some/path'
$ format='cat %f'
$ echo ${format//%f/$file}
cat /some/path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment