10

When I display the manual for pwd command, it says that long options like --physical are supported

$ man pwd
PWD(1)                           User Commands                          PWD(1)

NAME
       pwd - print name of current/working directory

SYNOPSIS
       pwd [OPTION]...

DESCRIPTION
       Print the full filename of the current working directory.

       -L, --logical
              use PWD from environment, even if it contains symlinks

       -P, --physical
              avoid all symlinks

However, it fails when I type the following

$ pwd --physical
-bash: pwd: --: invalid option
pwd: usage: pwd [-LP]

Why are long options not working for me?

I'm using RHEL 6.4. No alias for pwd is configured. Looks like it's standard pwd:

$ which pwd
/bin/pwd
  • 1
    The manual page is for a standalone utility which is not part of bash. – Thomas Dickey Jan 21 at 13:50
  • 7
    Use type instead of which, since which only shows commands, while type shows keywords, builtins, aliases, functions, and commands (and hashed command paths). For more details see Why not use “which”? What to use then? – wjandrea Jan 21 at 14:45
  • 1
    In my system man pwd has following note: your shell may have its own version of pwd, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports at the bottom of DESCRIPTION section. – Nykakin Jan 22 at 14:18
26

bash has a built-in command pwd which is what you are using when you simply type pwd into your shell.

To get the pwd as described by the manpage, you need force use of the external command. You can do this by specifying the full path to the executable (/bin/pwd in your case) or by prepending env before the line: env pwd, which starts the env command which can be used to add settings to the environment (but which is not done here) and then env starts the command specified. As env doesn't have a builtin pwd, the "real" /bin/pwd is executed.

The advantage of the builtin pwd in bash is that bash keeps track of the current directory, so getting the value is at zero cost, whereas the external command needs to search up through the filesystem to determine the path, which is much more IO intensive.

  • 4
    That's not how the external command actually works. unix.stackexchange.com/a/413225/5132 – JdeBP Jan 21 at 16:59
  • BTW: You can also disable the shell builtin (at least in bash) by enable -n pwd – derobert Jan 21 at 20:18
  • @JdeBP It used to be last time I looked (a long time ago :-) ); and whereas it now uses the getcwd() system call, the kernel still traverses the filesystem, which is easier to do in kernel space due to access to relevant data structures, As the linked answer states: "However, note that even on FreeBSD and those other operating systems the kernel does not keep track of the working directory with a string." – wurtel Jan 22 at 9:03
11

That manpage documents /bin/pwd, but when you run pwd you’re using the shell built-in; see the output of

type pwd

Your shell’s built-in pwd doesn’t support long options (see your shell’s documentation; since you’re using Bash, help pwd will provide a summary).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.