【问题标题】:No man page for the cd command没有 cd 命令的手册页
【发布时间】:2016-12-14 16:44:02
【问题描述】:

Ubuntu Linux 15.10 - 我刚刚注意到cd 没有手册页

这似乎有点奇怪。

我试过了:

man cd

在 cmd 行,我回来了

No manual entry for cd

我试图在

上查找文档
cd -

这对于在最后一个目录和当前目录之间翻转非常方便

cd --

这似乎是一个别名

cd ~

我在这里遗漏了一些非常明显的东西,还是应该提供手册页?

【问题讨论】:

    标签: linux shell command built-in ubuntu-15.10


    【解决方案1】:

    cd 不是命令,它内置在你的 shell 中。这是必要的,因为您当前的工作目录由以pwd 或“打印工作目录”命令命名的PWD 环境变量控制。

    父进程的环境变量不能被子进程更改。因此,如果您的 shell 运行 /bin/cd 更改了 PWD 它只会影响 /bin/cd 以及它运行的任何内容。它不会改变 shell 的PWD

    某些系统,如 OS X 和 CentOS,将 cd 手册页映射到 builtin,其中列出了所有 shell 内置项,并让您知道应该查看 shell 的手册页。

    你可以用echo $SHELL查看你的shell,可能是bash

    【讨论】:

    • 我不明白您对环境变量的解释与 cd 未在 man 上列出有什么关系。我可以更容易地要求进一步解释吗? :)
    • @SmartHumanism 一个cd 程序不能改变你的目录,所以它必须被内置到shell中,所以它不是一个程序。 man 是为程序设置的,所以 cd 没有手册页。即使我们决定无论如何都应该这样做,但每个 shell 都有一个稍微不同的cd。哪一个得到man cd
    • 感谢您的回复。我很感激。
    【解决方案2】:

    cd 是一个内置的 shell 命令。

    $ type cd
    cd is a shell builtin
    

    您可以在 Bash 上打开 cd 的帮助页面

    $ help cd
    

    目前显示的内容(Ubuntu 16.04):

    $ help cd
    cd: cd [-L|[-P [-e]] [-@]] [dir]
        Change the shell working directory.
    
        Change the current directory to DIR.  The default DIR is the value of the
        HOME shell variable.
    
        The variable CDPATH defines the search path for the directory containing
        DIR.  Alternative directory names in CDPATH are separated by a colon (:).
        A null directory name is the same as the current directory.  If DIR begins
        with a slash (/), then CDPATH is not used.
    
        If the directory is not found, and the shell option `cdable_vars' is set,
        the word is assumed to be  a variable name.  If that variable has a value,
        its value is used for DIR.
    
        Options:
            -L  force symbolic links to be followed: resolve symbolic links in
            DIR after processing instances of `..'
            -P  use the physical directory structure without following symbolic
            links: resolve symbolic links in DIR before processing instances
            of `..'
            -e  if the -P option is supplied, and the current working directory
            cannot be determined successfully, exit with a non-zero status
            -@  on systems that support it, present a file with extended attributes
                as a directory containing the file attributes
    
        The default is to follow symbolic links, as if `-L' were specified.
        `..' is processed by removing the immediately previous pathname component
        back to a slash or the beginning of DIR.
    
        Exit Status:
        Returns 0 if the directory is changed, and if $PWD is set successfully when
        -P is used; non-zero otherwise.
    

    很遗憾,它没有回答您的问题。然而,文档可以做到。

    你可以通过

    $ man builtins
    

    它会打开很多页帮助less,我的默认查看器。我可以通过按/ 键找到cd 的帮助,然后输入cd,然后输入Enter,然后按两次n 将我带到子字符串的第三个实例和帮助,内容如下:

       cd [-L|[-P [-e]] [-@]] [dir]
              Change  the  current  directory to dir.  if dir is not supplied,
              the value of the HOME shell variable is the default.  Any  addi‐
              tional arguments following dir are ignored.  The variable CDPATH
              defines the search path for the directory containing  dir:  each
              directory  name  in  CDPATH  is  searched  for dir.  Alternative
              directory names in CDPATH are separated by a colon (:).  A  null
              directory  name  in CDPATH is the same as the current directory,
              i.e., ``.''.  If dir begins with a slash (/), then CDPATH is not
              used.  The  -P  option  causes  cd to use the physical directory
              structure by resolving symbolic links while traversing  dir  and
              before processing instances of .. in dir (see also the -P option
              to the set builtin command); the -L option forces symbolic links
              to  be followed by resolving the link after processing instances
              of .. in dir.  If .. appears in dir, it is processed by removing
              the  immediately previous pathname component from dir, back to a
              slash or the beginning of dir.  If the  -e  option  is  supplied
              with  -P,  and  the current working directory cannot be success‐
              fully determined after a successful directory  change,  cd  will
              return  an unsuccessful status.  On systems that support it, the
              -@ option presents the extended  attributes  associated  with  a
              file  as  a directory.  An argument of - is converted to $OLDPWD
              before the directory change is attempted.  If a non-empty direc‐
              tory  name  from  CDPATH is used, or if - is the first argument,
              and the directory change is successful, the absolute pathname of
              the  new  working  directory  is written to the standard output.
              The return value is  true  if  the  directory  was  successfully
              changed; false otherwise.
    

    查找关于倒数第七行的- 参数:

    在尝试更改目录之前,- 的参数被转换为 $OLDPWD

    请注意,没有 -- 参数 - 这似乎意味着它实际上忽略了它。

    【讨论】:

      【解决方案3】:

      来自 bash 手册页的相关摘录,涵盖了 cd - 的用法

             cd [-L|[-P [-e]] [-@]] [dir]
                    Change the current directory to dir. 
                    ...
      
                    An argument of  -
                    is  converted to $OLDPWD before the directory change is attempted.  If a non-
                    empty directory name from CDPATH is used, or if - is the first argument,  and
                    the  directory change is successful, the absolute pathname of the new working
                    directory is written to the standard output.  The return value is true if the
                    directory was successfully changed; false otherwise.
      

      【讨论】:

      • 问题是为什么手册页不存在以及它在哪里。在cd 部分粘贴不是一个好的答案,它只适用于那个特定的外壳。是的,我知道你问了这个问题。
      • 它确实对 cd - 选项文档的 OP 部分有所帮助,但似乎没有提到 cd -- 不过.. 我想知道它是否真的是 cd 的别名~ ?
      • -- 是表示“选项结束”的通用方式,后面的所有内容都是文件或参数。处理以破折号开头的文件很方便。 cd -- 等价于 cd。而且,再一次,没有“cd 命令”,它内置在您的 shell 中,详细信息取决于您使用的 shell。例如,zsh 的行为不同。
      • 感谢您的额外澄清 - 我发现这个资源也很有帮助 - gnu.org/software/bash/manual/html_node/…
      猜你喜欢
      • 2014-09-29
      • 2023-03-07
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      • 2016-08-15
      • 2014-05-24
      相关资源
      最近更新 更多