Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 456
Ana Avatar asked Feb 17 '15 16:02

Ana


1 Answers

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.

like image 191
Etan Reisner Avatar answered Sep 21 '22 23:09

Etan Reisner