简体   繁体   English

“ / bin / bash cd〜”显示为“ / bin / bash:cd:没有这样的文件或目录”,为什么?

[英]“/bin/bash cd ~” results in “/bin/bash: cd: No such file or directory”, why?

The question is in the title. 问题在标题中。 I could not find anything on google, so im hoping someone here can explain this to me. 我在Google上找不到任何内容,因此希望这里有人可以向我解释。

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存在)

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/bash and the above mentioned if statement ends up in the false clause. 编辑:对不起,我应该更清楚地了解为什么我要编写/bin/bash cd ~而不是cd ~ :我正在使用#!/bin/bash编写bash脚本,上述if语句最终以错误条款。

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 cd command needs to be done within your running shell to be useful. 请注意, cd命令需要在正在运行的shell中完成才能使用。 When you change the working directory of a sub-shell, and then the sub-shell exits, you'll find yourself back where you started. 当您更改子外壳的工作目录,然后退出子外壳时,您会发现自己回到了开始的位置。

UPDATE : 更新

From within a bash script, you should be able to use the $HOME environment variable, which should consistently contain your home directory. 在bash脚本中,您应该能够使用$HOME环境变量,该变量应始终包含您的主目录。 I'm not aware what conditions would cause tilde expansion to fail, but I've always used $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. 详细的错误报告很方便,但是如果不需要它们,则使代码较小也有好处。

Assuming you did 假设你做了

$ /bin/bash cd ~

your shell interpreted cd as an argument to /bin/bash . 您的外壳将cd解释为/bin/bash的参数。 That syntax can eg be used to invoke shell scripts. 该语法可以例如用于调用Shell脚本。

What you most likely wanted was to change the current working directory. 您最可能想要的是更改当前工作目录。 In that case all you need is 在这种情况下,您所需要做的就是

$ cd ~ 

请尝试以下操作:

/bin/bash -c "cd ~"

Do not use quotes "" Example: 请勿使用引号""示例:

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

~ is not expanded within the quotes "" . ~不在引号""内扩展。 Use $HOME 使用$HOME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM