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?)
2. I think this is for the situation where the glob doesn't match, and the nullglob shell option is not set. Without that option, a non-matching glob is processed as a regular word. e.g. In an empty directory:
$ for file in ./*; do echo $file; done
./*
Note the glob pattern is printed by the echo statement. The -e test catches this condition.
Can someone explain the following:
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 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?)