Skip to content

Instantly share code, notes, and snippets.

@caruccio
Created April 24, 2012 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caruccio/2482538 to your computer and use it in GitHub Desktop.
Save caruccio/2482538 to your computer and use it in GitHub Desktop.
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