Comment

Comment by deathanatos

Today I learned globbing happens after word-splitting.

Can someone explain the following:

   for file in ./* ; do        # Prefix with "./*", NEVER begin with bare "*"
     if [ -e "$file" ] ; then  # Make sure it isn't an empty match
1. Why prefix with a "./" ? Is that just to help avoid the `cat $filename` scenario? (i.e., that $filename will be "./-n" instead of "-n", and that

  cat -- *
is perfectly valid?)

2. What's the -e check for? It says "an empty match" — -e means that the file exists, but * would only return files that exist, so -e must (with some caveats) be true. (The caveat being that there's a race condition between the globbing and the test, but with the added test, there's _still_ a race condition between the glob, the test, and the command execution. Are we just attempting to minimize the amount of race-condition by testing?)

parent deathanatos 11 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton