$ PWD vs. pwd regarding portability

I am writing a shell script that parses the path to the current working directory (prints like all base names above the current directory).

So far, I have used the PWD environment variable to analyze the path, but I wonder if

  • I can expect that PWD will always be installed
  • to give the same result on every platform

Would it be better to use a PWD shell? I need this script to work on as many platforms as possible, so I'm just wondering ...

+14
bash
source share
5 answers

POSIX requires $PWD be installed as follows:

 PWD 

This variable should represent the absolute path to the current working directory. It should not contain any components that are points or points. The value is set by the cd utility and the sh utility during initialization.

Thus, you can rely on what is installed - but notice "... the absolute path ..." rather than the absolute path.

bash (at least the latest versions) remembers which characters you followed when setting up $PWD (and the built-in pwd ). command pwd (i.e. an external command) will not. Thus, you will get different results, which may or may not be important to you. Use pwd -P if you need a path without symlinks.

Please note that the pwd documentation states:

If the application sets or disables the PWD value, pwd behavior is not specified.

So do not do this :)

In short, there is no winner. The environment variable will be in the POSIX shells, as well as the external command and possibly the built-in. Choose the one that best suits your needs, the important thing is whether you care about symbolic links or not.

+21
source share

From this forum article $ PWD vs `pwd` , which compares AIX 4.2.1, AIX 6, Sparc Solaris 10, and Redhat 5 in this regard:

  • there is no difference between $ PWD and embedded pwd,

  • there is no difference between the built-in pwd -P and / usr / bin / pwd.

The former shows the working directory with symbolic link names, while the latter shows the actual path.

The only mismatch is that the external command is located in / usr / bin on most systems and / bin on Redhat.

+3
source share

If you know that bash is available and the script runs with it, PWD is safe.

If only sh is available on some systems, use PWD .

+2
source share

If it were me, I would use pwd , as it is built-in for both bash and sh. This does not mean that they work the same in every way, but if you call it without parameters, it does not matter.

0
source share

Another point to pay attention to: command substitutions are usually unsafe at the end of a line .

This is obviously pretty far-fetched, but if you're really worried about safe input processing, you should use "$PWD" . See for example:

 $ my_dir=$'/tmp/trailing_newline\n' $ mkdir -p "$my_dir" $ cd "$my_dir" $ pwd /tmp/trailing_newline $ printf "%q\n" "$(pwd)" "$PWD" /tmp/trailing_newline $'/tmp/trailing_newline\n' $ cd "$(pwd)" sh: cd: /tmp/trailing_newline: No such file or directory $ cd "$PWD" 

You can bypass command substitution, but this is by no means beautiful. You can add a trailing character and then delete it with the parameter extension:

 $ pwd_guarded="$(pwd; printf '#')" $ pwd_fixed="${pwd_guarded%$'\n'#}" $ printf "%q\n" "$pwd_fixed" $'/tmp/trailing_newline\n' $ cd "$pwd_fixed" 

This is especially ugly because then you also need to remove the new pwd that pwd adds, which is usually removed by command substitution. It becomes a complete mess if you don't resort to non-POSIX constructs like $'' , so basically just use "$PWD" if you care about these things. Of course, it makes perfect sense to simply not support trailing newlines in directory names.

0
source share

All Articles