# Created on savesnippets.com ยท https://savesnippets.com/3JRsfhKaoaxrje s="hello-world-foo-bar" # Substring (offset, length) echo "${s:6}" # world-foo-bar echo "${s:6:5}" # world # Find & replace echo "${s/-/_}" # hello_world-foo-bar (first occurrence) echo "${s//-/_}" # hello_world_foo_bar (all occurrences) echo "${s/#hello/HI}" # HI-world-foo-bar (only if prefix) echo "${s/%bar/BAZ}" # hello-world-foo-BAZ (only if suffix) # Strip prefix / suffix file="path/to/config.json" echo "${file##*/}" # config.json (everything after last /) echo "${file%/*}" # path/to (everything before last /) echo "${file##*.}" # json (extension) echo "${file%.*}" # path/to/config (sans extension)