如何在bash中的字符串变量中阐明特殊语法字符
给定:
一个字符串变量,包含bash
用于扩展或作为定义者的字符,例如
> path="/this/path/contains whitespace/and/asterisk */myfile.txt"
目标:
我想在一个那些bash
语法元素被拒绝(或禁用但不简单地引用)的方式,即解决方案的输出
将是
> solution $path
/this/path/contains\ whitespace/and/asterisk\ \*/myfile.txt
问题:
不是bash
中有一个命令可以做到这一点,而不是必须自己与所有特殊角色挣扎?
GIVEN:
A string variable containing characters that are used by bash
for expansion or as delimiters, e.g.
> path="/this/path/contains whitespace/and/asterisk */myfile.txt"
GOAL:
I want to expand the variable in a way that those bash
syntax elements are backslashed (or disabled but not simply quoted), i.e. the output of solution
would be
> solution $path
/this/path/contains\ whitespace/and/asterisk\ \*/myfile.txt
QUESTION:
Isn't there a command in bash
that does that, rather than having to struggle with all special characters on my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
%Q
printf
的指定符(bash indendin):Use the
%q
specifier ofprintf
(a bash builtin):