LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep --include glob with shell variable (https://www.linuxquestions.org/questions/programming-9/grep-include-glob-with-shell-variable-843519/)

hashbang#! 11-10-2010 03:19 PM

grep --include glob with shell variable
 
Code:

grep -r $SEARCHDIR --include="day_$YYYY-$MM-*" -h -o -e PATTERN
The above command does exactly what I want: limit the search of files in $SEARCHDIR to those specified by --include.

I use double quotes rather than single quotes because of the shell variables. I would have thought this would cause the shell to expand the "*" but this doesn't seem to be the case. Can anyone explain?

neonsignal 11-10-2010 04:02 PM

Inside double quotes, the only special characters are dollar sign, the backticks, and the backslash.

The glob expansion is done at a different phase of the command parsing.

It wouldn't make sense to expand it inside quotes, because one purpose of the quotes is to prevent spaces from splitting up the parameter. Since the expansion will almost always have spaces, an expansion within quotes would mean a list would get treated as a single parameter, whether or not the spaces were in a filename or separating two filenames.

For example, suppose you had a directory with two files 'pic a.jpg' and 'pic b.jpg'. If the glob was expanded inside the quotes (which it isn't), then
Code:

"*"
would become
Code:

"pic a.jpg pic b.jpg"
which would not be a useful string, whether treated as a single filename or multiple ones (the spaces in the string do not all have the same meaning).


All times are GMT -5. The time now is 03:09 AM.