Here is my code:

if [ -f /Applications/xml[0-9].pl ]

I also tried with -f =~ and double brackets but none seem to work.

I first though that the regex might be faulty but I tried this.

echo h1 | sed 's/h[0-9]/h/'

And that worked. What am I doing wrong here?


2 Answers 2


You can't test a file with a regex nor a glob like this. You have to iterate over the files :

for file in /Applications/xml[0-9].pl; do if [ -f "$file" ]; then ...

bash expands the regular expression before evaluating the condition. If your directory has three files by the name


your if statement looks to shell as

if [ -f /Applications/xml0.pl /Applications/xml1.pl /Applications/xml2.pl ]]

which is syntactically incorrect. You will need to specify the filename completely when you want to look for its existence by using

for file in /Applications/xml[0-9].pl
    if [ -f ${file} ]

or if you can, use the find command to do whatever you want done with the file.

Not the answer you're looking for? Browse other questions tagged .