Bash provides nifty ways to slice, transform, and analyze strings. Hereβs how you can work with strings effectively.
${var#pattern}
β Remove shortest match from front${var##pattern}
β Remove longest match from front${var%pattern}
β Remove shortest match from end${var%%pattern}
β Remove longest match from end${#var}
β Get string length${var:offset:length}
β Extract substring${var/pattern/replacement}
β Replace first occurrence${var//pattern/replacement}
β Replace all occurrenceslength
, slice
, or replace
to see string magic.