Variable Pattern Matching Constructs
${var%pattern}
- Looks for a match to pattern at the end of $var. If it finds a match, it removes the shortest possible rightmost match.
- $var=file1.cpp ; echo ${var%.cpp}
- Output is “file1”.
- Matching uses command line files expansion wild cards: *, ?, [ ], [! ]
- Does not changes $var.
${var%%pattern}
- Looks for a match to pattern at the end of $var. If it finds a match, it removes the longest possible rightmost match.
- $var=file1rev123.cpp
- echo ${var%%1*.cpp}
- Output is “file”.
${var#pattern}
- Looks for a match to pattern at the beginning of $var. If it finds a match, it removes the shortest possible leftmost match.
- $var=file1.cpp
- echo ${var#f*.}
- Output is “cpp”.
${var##pattern}
- Looks for a match to pattern at the beginning of $var. If it finds a match, it removes the longest possible leftmost match.
- $var=file1.rev123.cpp
- echo ${var##f*.}
- Output is “cpp”.