LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2010, 03:19 PM   #1
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Rep: Reputation: 17
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?
 
Old 11-10-2010, 04:02 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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).

Last edited by neonsignal; 11-10-2010 at 04:17 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell glob element [:space:] not working as expected catkin Programming 2 07-25-2009 06:11 AM
grep a shell script variable contents kushalkoolwal Programming 8 02-04-2009 06:15 AM
help with glob - perl invoked from shell johngreg Programming 16 11-12-2008 03:33 PM
Shell script: How to include a variable between apostrophes within a command guarriman Programming 3 02-23-2007 03:12 AM
Variable names that include variables.... mychl Programming 7 09-24-2003 07:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration