In bash, how do I expand a wildcard while it's inside double quotes?

无人久伴 提交于 2019-11-26 08:33:50

问题


I would like to write the following function in bash:

go() {
  cd \"~/project/entry ${1}*\"
}

What this would do is to cd into a project subdirectory with prefix entry (note space) and possibly a long suffix. I would only need to give it a partial name and it will complete the suffix of the directory name.

So, if for example, I have the following folders:

~/project/entry alpha some longer folder name
~/project/entry beta another folder name
~/project/entry gamma

I can run go b and it will put me into ~/project/entry beta another folder name.

The problem is, of course, that the wildcard doesn\'t expand inside double quotes. I cannot omit the quotes because then I will not be able to capture the spaces properly.

How do I get the wildcard to expand while at the same time preserving the spaces?


回答1:


Move the quotes. Just don't quote the *. Probably also good not to quote the ~.

go() {
  cd ~/"project/entry ${1}"*
}

That being said if this matches more than one thing cd will use the first match and ignore all the other matches.



来源:https://stackoverflow.com/questions/28566599/in-bash-how-do-i-expand-a-wildcard-while-its-inside-double-quotes

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