Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在双引号路径引用中使用`~`_Bash_Quotes_Double Quotes_Spaces_Tilde Expansion - Fatal编程技术网

Bash 如何在双引号路径引用中使用`~`

Bash 如何在双引号路径引用中使用`~`,bash,quotes,double-quotes,spaces,tilde-expansion,Bash,Quotes,Double Quotes,Spaces,Tilde Expansion,此命令将导致错误 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 有什么区别?如果路径中有空格,我可以做些什么使“~”仍然包含在双引号内。只有在没有引号的情况下才展开波浪号。引用(或者

此命令将导致错误

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

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

只有在没有引号的情况下才展开波浪号。引用(或者,相当于在反斜杠前面加上引号)将禁用展开,并将它们转换为文字波浪形

允许在争论中开始和结束引用。您可以在不引用波浪号的同时引用空格。这些都是等效的:

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

只有当波浪线不带引号时,才会展开波浪线。引用(或者,相当于在反斜杠前面加上引号)将禁用展开,并将它们转换为文字波浪形

允许在争论中开始和结束引用。您可以在不引用波浪号的同时引用空格。这些都是等效的:

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

shell(
bash
)根据中的“瓷砖展开”规则展开瓷砖。如果引用字符串,则不执行展开,而是使用文字字符串。您可以直接转义空格,例如
wc-l~/foo\bar
(或者甚至只是包含空格的位,显然,例如
wc-l~/“foo-bar”/baz
),您可以使用
$HOME
而不是
~
,并将其包含在双引号中。这更适合于已经存在大量重复项的情况,shell(
bash
)根据中的“瓷砖展开”规则展开瓷砖。如果引用字符串,则不执行展开,而是使用文字字符串。您可以直接转义空格,例如
wc-l~/foo\bar
(或者甚至只是包含空格的位,显然,例如
wc-l~/“foo-bar”/baz
),您可以使用
$HOME
而不是
~
,并将其包含在双引号中。这更适合于已经存在大量重复项的情况,