aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Vitturi <vitturi.jan@gmail.com>2019-09-20 18:50:16 +0200
committerJan Vitturi <vitturi.jan@gmail.com>2019-09-20 18:50:16 +0200
commit02c30875695f0530b42ca497138f9c3f1043c090 (patch)
tree7dd4503f07a3283beef28d52380dd1c0b2cbd52e
parentd6d74706835d4a8220bda063c9af70793bfe268c (diff)
downloadpure-sh-bible-02c30875695f0530b42ca497138f9c3f1043c090.tar.gz
pure-sh-bible-02c30875695f0530b42ca497138f9c3f1043c090.zip
Check for unexpanded globs
-rw-r--r--README.md5
1 files changed, 4 insertions, 1 deletions
diff --git a/README.md b/README.md
index 263ff18..979af66 100644
--- a/README.md
+++ b/README.md
@@ -577,21 +577,24 @@ done < "file"
Don’t use `ls`.
-**CAVEAT:** When the glob does not match anything (empty directory or no matching files) the variable will contain the unexpanded glob.
+**CAVEAT:** When the glob does not match anything (empty directory or no matching files) the variable will contain the unexpanded glob. To avoid working on unexpanded globs check the existence of the file contained in the variable using the appropriate [file conditional](#file-conditionals). Be aware that symbolic links are resolved.
```shell
# Greedy example.
for file in *; do
+ [ -e "$file" ] || [ -L "$file" ] || continue
printf '%s\n' "$file"
done
# PNG files in dir.
for file in ~/Pictures/*.png; do
+ [ -f "$file" ] || continue
printf '%s\n' "$file"
done
# Iterate over directories.
for dir in ~/Downloads/*/; do
+ [ -d "$dir" ] || continue
printf '%s\n' "$dir"
done
```