在bash中,当通配符放在双引号中时,如何扩展? [英] In bash, how do I expand a wildcard while it's inside double quotes?

查看:93
本文介绍了在bash中,当通配符放在双引号中时,如何扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bash中编写以下函数:

I would like to write the following function in bash:

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

这将执行cd到项目子目录中,该子目录的前缀为entry(请注意空格),并且后缀可能很长.我只需要给它一个部分名称,它将完成目录名称的后缀.

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

我可以运行go b,它将使我进入~/project/entry beta another folder name.

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?

推荐答案

移动引号.只是不要引用*.也许也不要引用~.

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

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

也就是说,如果此匹配项不止一个,cd将使用第一个匹配项,而忽略所有其他匹配项.

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

这篇关于在bash中,当通配符放在双引号中时,如何扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆