Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

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

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...