Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One alternative:

  for name in subdir/*; do basename "$name"; done


Also 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


Note this string replacement is not anchored (right?) which can end up biting you badly (depending on circumstances of course).


It'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.


Thanks 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: