I want to check whether an input string refers to a file name – not a wildcard string like *.txt
.
This doesn't work:
if [ -f "$1" ];
Because $1
gets expanded to *.txt
, which gets expanded to, say foo.txt bar.txt
, which gets passed to test -f
.
Short of explicitly checking for wildcard characters, is there a general way to perform a shell substitution, and then prevent any globbing?
Best Answer
No, globs are not expanded when quoted, so:
Will check whether the file called
*.txt
in the current directory is a regular file or a symlink to a regular file.would be a problem (
$1
would undergo word splitting (here not doing anything with the default value of$IFS
) and filename generation).If you wanted to check whether there were regular files with a
.txt
extension, you'd need a loop or usefind
orzsh
glob qualifiers.Or if the pattern is found in
$1
:zsh
:find
: