BASH 找不到在路径中使用 ~/ 存在的隐藏文件
BASH cannot find hidden file that exists using ~/ in path
我有一个 BASH 脚本正在我的主目录中寻找隐藏文件。当我在尝试查找文件时指定绝对路径 /home/user/.foo
时,脚本会找到它,但如果我使用 ~/.foo
,则脚本找不到文件。有人可以解释为什么会发生这种情况以及我可以通过其他什么方式找到该文件。我希望能够在不同用户的主目录中找到该文件,而不仅仅是我自己的主目录。
很抱歉没有提供代码片段,但这里是
file="~/.foo"
[ -f $file ] && echo "foo exists!"
然而这有效
file="home/user/.foo"
[ -f $file ] && echo "foo exists!"
始终提供不起作用的代码。根据您的描述无法复制:
$ ls ~/.foo
/home/user/.foo
正如 anishsane 所建议的那样,可能会有引号。
$ ls "~/.foo"
ls: cannot access ~/.foo: No such file or directory
但是如何处理白色space?将 ~ 放在引号外:
$ ls ~/".f o o"
/home/user/.f o o
但是引号外也要加/,否则~不展开
$ ls ~"/.f o o"
ls: cannot access ~/.f o o: No such file or directory
这是因为当波浪号是 单词 中的第一个未加引号的字符时,shell 仅执行 波浪号扩展。在
file="~/.foo"
波浪号用双引号引起来,因此未展开。最佳做法是始终使用 $HOME
当波浪号应该扩展到您的主目录(而不是像 ~user
.
中那样的其他用户的主目录)
来自POSIX的直接涂料:
2.6.1 Tilde Expansion
A "tilde-prefix" consists of an unquoted <tilde> character at the
beginning of a word, followed by all of the characters preceding the
first unquoted <slash> in the word, or all the characters in the word
if there is no <slash>. In an assignment (see XBD Variable
Assignment), multiple tilde-prefixes can be used: at the beginning of
the word (that is, following the <equals-sign> of the assignment),
following any unquoted <colon>, or both. A tilde-prefix in an
assignment is terminated by the first unquoted <colon> or <slash>. If
none of the characters in the tilde-prefix are quoted, the characters
in the tilde-prefix following the <tilde> are treated as a possible
login name from the user database. A portable login name cannot
contain characters outside the set given in the description of the
LOGNAME
environment variable in XBD Other Environment Variables. If
the login name is null (that is, the tilde-prefix contains only the
tilde), the tilde-prefix is replaced by the value of the variable
HOME
. If HOME
is unset, the results are unspecified. Otherwise, the
tilde-prefix shall be replaced by a pathname of the initial working
directory associated with the login name obtained using the getpwnam()
function as defined in the System Interfaces volume of POSIX.1-2008.
If the system does not recognize the login name, the results are
undefined.
The pathname resulting from tilde expansion shall be treated as if
quoted to prevent it being altered by field splitting and pathname
expansion.
我有一个 BASH 脚本正在我的主目录中寻找隐藏文件。当我在尝试查找文件时指定绝对路径 /home/user/.foo
时,脚本会找到它,但如果我使用 ~/.foo
,则脚本找不到文件。有人可以解释为什么会发生这种情况以及我可以通过其他什么方式找到该文件。我希望能够在不同用户的主目录中找到该文件,而不仅仅是我自己的主目录。
很抱歉没有提供代码片段,但这里是
file="~/.foo"
[ -f $file ] && echo "foo exists!"
然而这有效
file="home/user/.foo"
[ -f $file ] && echo "foo exists!"
始终提供不起作用的代码。根据您的描述无法复制:
$ ls ~/.foo
/home/user/.foo
正如 anishsane 所建议的那样,可能会有引号。
$ ls "~/.foo"
ls: cannot access ~/.foo: No such file or directory
但是如何处理白色space?将 ~ 放在引号外:
$ ls ~/".f o o"
/home/user/.f o o
但是引号外也要加/,否则~不展开
$ ls ~"/.f o o"
ls: cannot access ~/.f o o: No such file or directory
这是因为当波浪号是 单词 中的第一个未加引号的字符时,shell 仅执行 波浪号扩展。在
file="~/.foo"
波浪号用双引号引起来,因此未展开。最佳做法是始终使用 $HOME
当波浪号应该扩展到您的主目录(而不是像 ~user
.
来自POSIX的直接涂料:
2.6.1 Tilde Expansion
A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>. In an assignment (see XBD Variable Assignment), multiple tilde-prefixes can be used: at the beginning of the word (that is, following the <equals-sign> of the assignment), following any unquoted <colon>, or both. A tilde-prefix in an assignment is terminated by the first unquoted <colon> or <slash>. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the <tilde> are treated as a possible login name from the user database. A portable login name cannot contain characters outside the set given in the description of the
LOGNAME
environment variable in XBD Other Environment Variables. If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variableHOME
. IfHOME
is unset, the results are unspecified. Otherwise, the tilde-prefix shall be replaced by a pathname of the initial working directory associated with the login name obtained using thegetpwnam()
function as defined in the System Interfaces volume of POSIX.1-2008. If the system does not recognize the login name, the results are undefined.The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.