aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorblack <dylan.araps@gmail.com>2019-09-20 19:59:07 +0300
committerGitHub <noreply@github.com>2019-09-20 19:59:07 +0300
commitbef1e1dc2592b1b630c4f5c492338a9e38d63975 (patch)
tree946856f9296f7a459426db75ce8aeccaba5f4df8
parent9f252b5aa58224ad21a26db4c13287d9cd471667 (diff)
parent02c30875695f0530b42ca497138f9c3f1043c090 (diff)
downloadpure-sh-bible-bef1e1dc2592b1b630c4f5c492338a9e38d63975.tar.gz
pure-sh-bible-bef1e1dc2592b1b630c4f5c492338a9e38d63975.zip
Merge pull request #3 from jan4843/patch-1
Add caveats for unexpanded globs
-rw-r--r--README.md7
1 files changed, 7 insertions, 0 deletions
diff --git a/README.md b/README.md
index a91f9c3..2edfe63 100644
--- a/README.md
+++ b/README.md
@@ -439,6 +439,8 @@ $ lines ~/.bashrc
This works by passing the output of the glob to the function and then counting the number of arguments.
+**CAVEAT:** When the glob does not match anything (empty directory or no matching files) it is not expanded and the function returns `1`.
+
**Example Function:**
```sh
@@ -575,19 +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. 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
```