5

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.

| improve this question | | | | |
10

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.

| improve this answer | | | | |
  • 1
    @JeffM: Note, that it has nothing at all to do with find. It's a shell thing. – user unknown Apr 28 '12 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 '12 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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