Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • That's misleading [ -e "$1" ] doesn't check whether files were given, but whether the first argument is an accessible file (after symlink resolution). Even if you change it to [ ! -e "$1" ] && [ ! -L "$1" ], considering that *.txt expansion is done against what is returned by readdir() and doesn't check whether the files are accessible, so there'll be corner cases where *.txt expands to files some of which are not accessible, not to mention the race condition when the file disappears in between the glob expansion and [ is run. Commented Nov 25, 2022 at 9:21
  • @StéphaneChazelas The main focus of the question is to handle the case where the filename glob is not matching anything. If the glob does not match anything, the first argument to the script will not correspond to an existing name (it will be the unexpanded pattern). I don't think we can do much to prevent race conditions, but if the pattern expands to unaccessible names, then this falls outside the scope of the question (EDIT: actually, it may not, but I'm uncertain how to fix it without making it super convoluted and difficult to understand). Commented Nov 25, 2022 at 10:48
  • Note that in bash failglob in scripts works like at the prompt of an interactive shell. It doesn't exit the shell, it exits the subshell if any and otherwise returns to the would be prompt if the shell was interactive (the next command to read). For instance. printf '%s\n' 'echo /+*' 'echo 1' '{' ' echo /+*' ' echo 2' '}' 'echo 3' '(/bin/echo /+*); echo 4' | bash -O failglob still outputs 1, 3 and 4. Also note that failglob takes precedence over nullglob, another reason why it's hardly usable. Not to mention that globbing applies to unquoted expansions there! Commented Nov 25, 2022 at 10:56
  • @StéphaneChazelas Thanks, that was a misconception on my part, because I was testing with bash -O failglob -c 'echo *boo*; echo ok' (ok is not outputted). Commented Nov 25, 2022 at 11:58