Merge pull request #3 from jan4843/patch-1

Add caveats for unexpanded globs
This commit is contained in:
black 2019-09-20 19:59:07 +03:00 committed by GitHub
commit bef1e1dc25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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"
Dont 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
```