"〜/桌面/的test.txt:没有这样的文件或目录"
我已经写了这个剧本:
#!/bin/bash
file="~/Desktop/test.txt"
echo "TESTING" > $file
该脚本不工作;它给了我这个错误:
The script doesn't work; it gives me this error:
./tester.sh: line 4: ~/Desktop/test.txt: No such file or directory
我在做什么错了?
What am I doing wrong?
试着用替换
。波浪线扩展,只有当波浪线是带引号的发生。参见〜
$ HOME 信息(bash)的波浪线扩展
。
Try replacing ~
with $HOME
. Tilde expansion only happens when the tilde is unquoted. See info "(bash) Tilde Expansion"
.
您也可以做文件=〜/桌面
没有引用它,但如果你的东西,在这一个字段分隔符替换其中的一部分,那么它会打破。引用变量的值可能是一个好东西进入反正习惯。引用变量文件=〜/桌面
也将工作,但我认为这是相当丑陋。
You could also do file=~/Desktop
without quoting it, but if you ever replace part of this with something with a field separator in it, then it will break. Quoting the values of variables is probably a good thing to get into the habit of anyway. Quoting variable file=~/"Desktop"
will also work but I think that is rather ugly.
另一个原因是preFER $ HOME
,在可能的情况:波浪线扩展只发生在文字的开端。因此,命令--option =〜/富
只会工作,如果命令
做波浪线扩展本身,这将是命令而异,而命令--option =$ HOME /富
永远是可行的。
Another reason to prefer $HOME
, when possible: tilde expansion only happens at the beginnings of words. So command --option=~/foo
will only work if command
does tilde expansion itself, which will vary by command, while command --option="$HOME/foo"
will always work.