【问题标题】:How to use `~` inside a double quote path reference [duplicate]如何在双引号路径引用中使用`~`[重复]
【发布时间】:2021-07-11 00:40:40
【问题描述】:

这个命令会报错

wc -l "~/tmp.txt"
wc: '~/tmp.txt': No such file or directory

虽然这些命令运行良好

wc -l ~/tmp.txt
14 /home/user/tmp.txt

wc -l "/home/user/tmp.txt"
14 /home/user/tmp.txt

有什么区别?如果路径中有空格,我可以做些什么来在双引号中包含“~”吗?

【问题讨论】:

标签: bash quotes double-quotes spaces tilde-expansion


【解决方案1】:

波浪号仅在未加引号时才会展开。引用(或者,等效的,在反斜杠前面)禁用扩展并将它们变成文字波浪号。

可以在参数中间开始和结束引号。您可以在不加引号的情况下引用空格。这些都是等价的:

wc -l ~/"file name with spaces.txt"
wc -l ~/'file name with spaces'.txt
wc -l ~/file\ name\ with\ spaces.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 2018-04-03
    • 1970-01-01
    • 2012-01-06
    • 2011-07-13
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多