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

查看:27
本文介绍了在 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 另一个文件夹名称.

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天全站免登陆