Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 10 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.

share|improve this answer
    
Thank you. That was the simple thing I was looking for. –  JeffM Apr 27 '12 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 '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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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