问题
When I do the following:
rmdir /path/to/dir/*.lproj
it works fine.
When I do:
APP_RESOURCES="/path/to/dir"
rmdir "$APP_RESOURCES/*.lproj"
It doesn't work and says it literally looks for a directory named with an astrix-symbol. The wildcard doesn't get interpreted. How can this be made to work within quotations?
回答1:
Expansion is handled by the shell so you will have to write something like this:
APP_RESOURCES="/path/to/dir"
rmdir "${APP_RESOURCES}/"*.lproj
回答2:
No globbing is done inside double quotes. Do
APP_RESOURCES="/path/to/dir"
rmdir "$APP_RESOURCES"/*.lproj
See this [ question ] for some detail.
回答3:
You are quoting your variable expansions, keep doing that. Bad thing is that you try to quote your globbing. For the shell to glob you shouldn't quote:
app_resource="/path/to/dir"
rmdir "$app_resource"/*.lproj
This will however still expand to /path/to/dir/*.lproj
in cases where no match is found, consider this:
% app_resource="/path/to/dir"
% echo "$app_resource/something/that/doesnt/exists/"*.abc
/path/to/dir/something/that/doesnt/exists/*.abc
One way around that is to check if the file/directory exists:
app_resource="/path/to/dir"
dir_to_remove="$app_resource/"*.lproj
[ -d "$dir_to_remove" ] && rmdir "$dir_to_remove"
来源:https://stackoverflow.com/questions/37563513/wildcard-within-quotations