"〜/桌面/的test.txt:没有这样的文件或目录" [英] "~/Desktop/test.txt: No such file or directory"

查看:157
本文介绍了"〜/桌面/的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.

这篇关于"〜/桌面/的test.txt:没有这样的文件或目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆