Hacker News new | ask | show | jobs
by inopinatus 2093 days ago
find(1) is super useful for any hierarchical or filtering case, and I use it often, but it can also be arcane and unwieldy. In many circumstances your cwd shell iterator can be simpler:

    for fname in *; do echo "$fname"; done
This skips dotfiles, of course, but that’s intentional. The point of dotfiles is to be skipped.

With bash or on GNU systems try

    printf "%q\n" "$fname"
instead of echo, to obtain an escaped string.
2 comments

I thought that it would iterate over space-separated tokens. But it works, some magic here, thanks, that's definitely the proper way to go.
It's because the result of a glob is not immediately subjected to shell expansion.
won't this echo "*" if there are no files in the directory?

I always have to look up the 'safe iteration' invocation when iterating files in a directory, because it involves jumping through a few more hoops than is really reasonable.

Yes. You can change the behaviour in bash by setting the nullglob option (shopt -s nullglob).