“/bin/bash cd ~”导致“/bin/bash: cd: No such file or directory”,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12133209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:07:21  来源:igfitidea点击:

"/bin/bash cd ~" results in "/bin/bash: cd: No such file or directory", why?

linuxbash

提问by Andy

The question is in the title. I could not find anything on google, so im hoping someone here can explain this to me.

问题在标题中。我在谷歌上找不到任何东西,所以我希望这里有人可以向我解释这一点。

I am using debian 6.0.5 and the shell assigned to the executing user in the /etc/passwd file is /bin/bash

我使用的是 debian 6.0.5,在 /etc/passwd 文件中分配给执行用户的 shell 是 /bin/bash

So, simply writing cd ~works and brings me to the users home directory.

因此,只需编写cd ~作品并将我带到用户主目录。

test -d "~/some_dir"returns false in an if statement ( some_dir exsits )

test -d "~/some_dir"在 if 语句中返回 false ( some_dir exsits )

Edit: Sorry I should've been more clear as of why I was writing /bin/bash cd ~instead of cd ~: I am writing a bash script with #!/bin/bashand the above mentioned if statement ends up in the false clause.

编辑:对不起,我应该更清楚为什么我要写/bin/bash cd ~而不是cd ~:我正在写一个 bash 脚本,#!/bin/bash上面提到的 if 语句以 false 子句结尾。

回答by Benjamin Bannier

Assuming you did

假设你做了

$ /bin/bash cd ~

your shell interpreted cdas an argument to /bin/bash. That syntax can e.g. be used to invoke shell scripts.

您的 shell 被解释cd/bin/bash. 例如,该语法可用于调用 shell 脚本。

What you most likely wanted was to change the current working directory. In that case all you need is

您最有可能想要的是更改当前工作目录。在这种情况下,您只需要

$ cd ~ 

回答by francoisrv

Try the following:

请尝试以下操作:

/bin/bash -c "cd ~"

回答by ghoti

The options for any command line are expanded before the command is run, even for internal commands. Whatever shell you're using to run /bin/bash cd ~is presumably interpreting the tilde literally rather than a special character that expands to your home directory.

任何命令行的选项都会在命令运行之前展开,即使对于内部命令也是如此。无论您使用什么 shell 来运行/bin/bash cd ~,大概都是按字面解释波浪号,而不是扩展到主目录的特殊字符。

As a test, try creating a directory by that name and see if the error goes away.

作为测试,尝试使用该名称创建一个目录,然后查看错误是否消失。

> mkdir ./~
> /bin/bash cd ~

Note that the cdcommand needs to be done within your running shellto be useful. When you change the working directory of a sub-shell, and then the sub-shell exits, you'll find yourself back where you started.

请注意,该cd命令需要在您正在运行的 shell 中完成才能有用。当您更改子 shell 的工作目录,然后子 shell 退出时,您会发现自己回到了开始的地方。

UPDATE:

更新

From within a bash script, you should be able to use the $HOMEenvironment variable, which should consistently contain your home directory. I'm not aware what conditions would cause tilde expansion to fail, but I've always used $HOME.

在 bash 脚本中,您应该能够使用$HOME环境变量,它应该始终包含您的主目录。我不知道什么条件会导致波浪号扩展失败,但我一直使用$HOME.

Also, when determining whether you can change into a particular directory, you have the option of being explicit and returning useful status:

此外,在确定是否可以更改到特定目录时,您可以选择明确并返回有用的状态:

unset err
if [[ ! -d "$somedir" ]]; then
  err="Can't find $somedir"
elif [[ ! -r "$somedir" ]]; then
  err="Can't read $somedir"
fi

if [[ -n "$err" ]]; then
  echo "$ERROR: $err" >&2
  exit 1
fi

cd "$somedir"

Or, you can just try the CD and look at its results.

或者,您可以尝试 CD 并查看其结果。

if ! cd "$somedir"; then
  echo "ERROR: $somedir not availble"
  exit 1
fi

Detailed error reports are handy, but if you don't need them, keeping your code small has advantages as well.

详细的错误报告很方便,但如果您不需要它们,保持代码小也有好处。

回答by Federico

Do not use quotes ""Example:

不要使用引号""示例:

$ test -d ~/.aptitude
$ echo $?
0
$ test -d "~/.aptitude"
$ echo $?
1

~is not expanded within the quotes "". Use $HOME

~没有在引号内展开""。用$HOME