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.

link|improve this question
feedback

1 Answer

up vote 8 down vote accepted

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

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

link|improve this answer
Thank you. That was the simple thing I was looking for. – JeffM Apr 27 at 22:42
1  
@JeffM: Note, that it has nothing at all to do with find. It's a shell thing. – user unknown Apr 28 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 at 11:20
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.