helpermethod
helpermethod

Reputation: 62244

$PWD vs. pwd regarding portability

I'm writing a shell script which parses the path of the current working directory (printing a like of all basenames above the current directory).

So far, I've been using the environment variable PWD to parse the path but I wonder if

Would it possibly be better to use the pwd shell-builtin? I need this script to run on as many platforms as possible, so I just wondered...

Upvotes: 25

Views: 11624

Answers (5)

Izaak van Dongen
Izaak van Dongen

Reputation: 2545

Another point to note is command substitutions are not generally safe on trailing newlines .

This is obviously fairly contrived, but if you're really concerned about safely handling input you should be using "$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"

It is possible to work around the command substitution but it is by no means pretty. You can append a trailing character and then strip it with a parameter expansion:

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

This is particularly ugly because you then also have to strip the newline that pwd adds, which would normally have been stripped by the command substitution. This becomes a total 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 is perfectly reasonable to just not support trailing newlines in directory names.

Upvotes: 3

Mat
Mat

Reputation: 206851

POSIX requires $PWD to be set in the following fashion:

PWD  

This variable shall represent an absolute pathname of the current working directory. It shall not contain any components that are dot or dot-dot. The value is set by the cd utility, and by the sh utility during initialization.

So you can rely on that being set – but do note "... an absolute path...", not the absolute path.

bash (at least recent versions) will remember what symlinks you followed when setting $PWD (and the pwd builtin). command pwd (that is, the external command) will not. So you'll get different results there, which might, or might not, be important for you. Use pwd -P if you want a path without symlinks.

Do note that the pwd documentation states:

If an application sets or unsets the value of PWD, the behavior of pwd is unspecified.

So, don't do that :)

In short, there is no winner here. The environment variable will be there in POSIX shells, as will the external command and possibly a built-in too. Choose the one that best fits your need, the important thing being whether you care about symlinks or not.

Upvotes: 33

mouviciel
mouviciel

Reputation: 67859

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

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

  • there is no difference between builtin pwd -P and /usr/bin/pwd.

The former shows working directory with names of symbolic links whereas the latter displays actual path.

The only discrepancy is that external command is in /usr/bin in most systems and /bin in Redhat.

Upvotes: 4

Karoly Horvath
Karoly Horvath

Reputation: 96276

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

If, on some systems, only plain sh is available, use pwd.

Upvotes: 2

wholerabbit
wholerabbit

Reputation: 11566

If it were me, I'd use pwd since it is a built-in both for bash and sh. That does not mean they work identically in all respects, but if you are invoking it without options, that shouldn't matter.

Upvotes: 1

Related Questions