Expansion and Data
String Cleanup
Paths and Names
Parameter expansion can remove path prefixes, trim suffixes, and replace characters without spawning another command.
Program
Play the script to watch a path become a filename, then a report name, then a shell-safe slug.
string_cleanup.sh
#!/usr/bin/env bash
path="/var/log/app/access log.txt"
file="${path##*/}"
name="${file%.*}"
slug="${name// /_}"
echo "$slug"
prefix removal
`${path##*/}` removes the longest prefix ending in `/`, leaving a filename.
suffix removal
`${file%.*}` removes the shortest suffix that starts with a dot.
substitution
`${name// /_}` replaces every space with an underscore.