Skip to content

Instantly share code, notes, and snippets.

@hsuh
Last active August 29, 2015 14:15
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 hsuh/f08c526c9c39e9646357 to your computer and use it in GitHub Desktop.
Save hsuh/f08c526c9c39e9646357 to your computer and use it in GitHub Desktop.
Bash pattern matching
$ a=/cygdrive/c/projects/sandbox/projects
##match pattern from the left hand side. Double # means greedy.
hsuh@hsuh /cygdrive/c/projects/sandbox/projects
$ echo ${a##*sandbox}
/projects
##match pattern from the right hand side. Double % means greedy.
hsuh@hsuh /cygdrive/c/projects/sandbox/projects
$ echo ${a%%proj*}
/cygdrive/c/
##match pattern from the right hand side. Remove the matched pattern. Return the rest.
hsuh@hsuh /cygdrive/c/projects/sandbox/projects
$ echo ${a%proj*}
/cygdrive/c/projects/sandbox/
More awesomeness here - http://www.tldp.org/LDP/abs/html/parameter-substitution.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment