在 bash 中嵌套引号
我想在 bash 中执行类似的操作:
alias foo='bar="$(echo hello world | grep \"hello world\")"; echo $bar;'; foo
预期输出:hello world
输出:grep: world": 没有这样的文件或目录
外引号必须是单引号,双引号 $bar 将为空。
接下来的引号必须是双引号,单引号 $() 不会扩展。
内部引号可以是两种类型的引号,但单引号不允许在其中使用单引号。
我该如何实现这一点?
I want to something like this in bash:
alias foo='bar="$(echo hello world | grep \"hello world\")"; echo $bar;'; foo
Expected output: hello world
Ouput: grep: world": No such file or directory
The outer quotes have to be single quotes, with double quotes $bar would be empty.
The next quotes have to be double quotes, with single quotes $() wouldn't expand.
The inner quotes could be both type of quotes, but single quotes doesn't allow single quotes inside of them.
How to I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
$()
里面的东西代表一个子shell,所以你可以在里面放置未转义的双引号The stuff inside
$()
represents a subshell, so you are allowed to place un-escaped double quotes inside有点不清楚“类似这样的东西”是什么意思,但实现这里似乎要点的最简单方法是一个简单的函数:
It's a bit unclear what "something like this" means, but the simplest way to achieve what seems to be the point here is a simple function:
逃离空间
Escape the spaces
$()
周围的双引号不是必需的:The double quotes around
$()
are not necessary: