I am seeing different results from the find command between being run manually on the command line vs run in a script.

If I run the following on the command line:

find . -name '*.txt' ! -name '*20120427.txt' 

I get all the .txt files in a directory except for the ones with the date that I want to avoid.

But when I have this same line in a script with a variable to identify the date:

FDATE=`date +%Y%m%d`
find . -name '*.txt' ! -name '*${FDATE}.txt' 

The script finds all the .txt files including the ones with the date I want to avoid.

The only difference between the 2 is that I am passing the FDATE variable in the script. Is there something obvious I am missing? I do not understand why the results are coming out different.

1 Answer 1


Change the single-quotes in '*${FDATE}.txt' to double quotes: "*${FDATE}.txt"

Unix shells don't do variable substitution inside single-quoted string literals, but they will do it inside double-quoted string literals.

  • 1
    @JeffM: Note, that it has nothing at all to do with find. It's a shell thing. Apr 28, 2012 at 2:17
  • tip: when troubleshooting, run the exact same commands in a shell, in this case set the the FDATE variable and run the command exactly as it is written in the script.
    – Bram
    Apr 29, 2012 at 11:20

You must log in to answer this question.

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