5 ms·
One alternative: for name in subdir/*; do basename "$name"; done
by rascul 2y ago
One alternative:
for name in subdir/*; do basename "$name"; done
- Izkata 2y agoAlso since subdir is hardcoded, you can reliably type it a second time to chop off however much of the start you want: for name in subdir/subsubdir/*; do echo "${name#subdir/}" # subsubdir/foo done
- xolox 2y agoNote this string replacement is not anchored (right?) which can end up biting you badly (depending on circumstances of course).
- chuckadams 2y agoIt's anchored on the left. ${name#subdir/} will turn 'subdir/abc' into 'abc', but will not touch foo/subdir/bar. I don't think bash even has syntax to replace in the middle of an expansion, I always pull out sed for that.
- xolox 2y agoThanks for clarifying, I learned something new today! Edit: It turns out that Bash does substitutions in the middle of strings using the ${string/substring/replacement} and ${string//substring/replacement} syntax, for more details see https://tldp.org/LDP/abs/html/string-manipulation.html https://tldp.org/LDP/abs/html/string-manipulation.html