命令替换但不将输出分解为多个参数
有没有办法在 BASH shell 中进行命令替换而不将输出分解为多个参数?
我将某个目录的路径(从 GUI 文件浏览器中的位置栏)复制到剪贴板,然后发出以下命令,其中命令 xsel 返回剪贴板内容,在本例中是目录的路径
cd `xsel`
:包含空格,甚至可能包含 BASH 使用的一些特殊字符。
如何将命令的输出作为单个参数传递,并且 BASH 不会弄乱特殊字符?
Is there a way to do command substitution in BASH shell without breaking output into multiple arguments?
I copy the path of some directory (from the location bar in a GUI file browser) to clipboard and then issue the following command, where the command xsel returns the clipboard content, which is the path of the directory in this case:
cd `xsel`
But some path contain spaces or may even contain some special characters used by BASH.
How can I pass the output of a command as a single argument and without BASH messing with special characters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎可以处理所有特殊字符(包括 $ 和空格)。
我的测试字符串是
boo*;cd.*($\: $_
seems to handle all special characters (including $ and spaces).
My test string was
boo*;cd.*($\: $_
您是否尝试过:
这应该可以完成工作,除非您的路径中有美元($)或反斜杠(\)。
Have you tried:
That should do the job, unless you have dollars($) or back-slashes (\) in your path.
如果您不以编程方式执行此操作,Linux 中的大多数终端都允许您通过单击鼠标中键从剪贴板进行粘贴。当然,您仍然需要在粘贴之前和之后添加引号,就像 @dave 建议的那样。
If you aren't doing this programmatically, most terminals in Linux let you paste from the clipboard with a middle-click on your mouse. Of course, you'll still need to put quotes before and after your paste, like @dave suggests.