Skip to content

Instantly share code, notes, and snippets.

@kopwei
Created October 9, 2013 16:28
Show Gist options
  • Save kopwei/6904021 to your computer and use it in GitHub Desktop.
Save kopwei/6904021 to your computer and use it in GitHub Desktop.
Bash string cutting with delimiter
Pattern Matching
In pattern matching, you can match from the left or from the right. The operators, along with their functions and examples, are shown below:
Operator: ${foo#t*is}
Function: deletes the shortest possible match from the left
Example:
$ export foo="this is a test"
$ echo ${foo#t*is}
is a test
$
Operator: ${foo##t*is}
Function: deletes the longest possible match from the left
Example:
$ export foo="this is a test"
$ echo ${foo##t*is}
a test
$
Operator: ${foo%t*st}
Function: deletes the shortest possible match from the right
Example:
$ export foo="this is a test"
$ echo ${foo%t*st}
this is a
$
Operator: ${foo%%t*st}
Function: deletes the longest possible match from the right
Example:
$ export foo="this is a test"
$ echo ${foo%%t*st}
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment