3 ms·
I admit to being guilty of the odd for i in $(ls) do Something dumb done
by chris_wot 8y ago
I admit to being guilty of the odd
for i in $(ls)
do
Something dumb
done
- myrandomcomment 8y agoIt is like you read my script.sh over my shoulder as I was writing it. :)
- zaphirplane 8y agoOut it interest why not xarg or find’s exec
- freedomben 8y agoNot GP, but I often find myself writing making statements (which is awkward with find), or using syntax that find struggles with.
- xorcist 8y agoIt clearly depends on what good that subshell running ls does for you, but the trivial form would be: for i in *txt do ... done The reason to stuff every file name through ls (keep in mind that it's always your shell doing glob expansions, ls is not involved) would be to sort them by creation date or some other processing that ls can do. But as soon as there are many files involved, or those sorting arguments gets non-trivial, it will fall apart completely. Use find | while read instead, or xargs. That's going to be easier on the eyes and actually work.
- wahern 8y agofor i in *; do ...; done works even better as it can handle files with whitespace. I usually disable file globbing but enable it specifically for code like this. EDIT: Actually, one issue with the above is that if there aren't any matching files then you get a literal "*". But I'd rather deal with that then break when filenames have embedded whitespace.