Neither [ -f ... ](nor [[ -f ... ]]other file verification operators) are designed to work with templates (aka globs, wildcard expression) . They always interpret their operand as a literal file name. [1]
A simple trick is to check whether the template (glob) matches exactly one file , use a helper function :
existsExactlyOne() { [[ $# -eq 1 && -f $1 ]]; }
if existsExactlyOne *."$1".*.ext; then
If you are just wondering if there are any matches, i.e. one or more , the function is even simpler:
exists() { [[ -f $1 ]]; }
If you want to avoid a function , it becomes more complex :
. , , ( .)
if [[ $(shopt -s nullglob; set -- *."$1".*.ext; echo $#) -eq 1 ]]; then
- (
$(...)) :shopt -s nullglob bash ,set -- ... ($1, $2,...) , .echo $# , ;
- ( stdout )
-eq, () 1.
, , , .. , -eq -ge.
[1]
@Etan Reisinger , [ ... ] ( ) , -f ( ).
bash [[ ... ]], - (.. ).
, ( ) :
[[ ... ]] : .[ ... ] , .- :
- ,
nullglob ( ), , nullglob , true, -f, - , ( )).
- :
[ ... ] , , .