Wildcard within quotations

北战南征 提交于 2021-01-04 07:14:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!