According to "chdir" xopen specification, using an empty string ("") as argument should results in an error (enoent):
[ENOENT]
A component of path does not name an existing directory or path is an empty string.
I've checked many different combinations of OSes and shells using command;
cd ""
which eventually calls "chdir" system call, with argv == 2, and argv[1] pointing to an empty string.
The results is that only some ksh93 (not all versions) on Linux (not on AIX) returns an error. "/bin/sh" always success but on AIX it moves to $HOME and on linux cwd is unchanged
Why so many differences?
As suggested previously, you can trace through the cd open group page to find the behavior (my notes are the bullet-points):
<slash> character, set curpath to the operand and proceed to step 7.
<colon>-separated pathnames of CDPATH (see the ENVIRONMENT VARIABLES section) if the pathname is non-null, test if the concatenation of that pathname, a <slash> character if that pathname did not end with a <slash> character, and the directory operand names a directory. If the pathname is null, test if the concatenation of dot, a <slash> character, and the operand names a directory. In either case, if the resulting string names an existing directory, set curpath to that string and proceed to step 7. Otherwise, repeat this step with the next pathname in CDPATH until all pathnames have been tested.
/foo:/bar:/baz and /foo does not exist, cd will first try /foo/ and fail this step. It will then try /bar/. If /bar exists as a directory, it will set curpath to /bar/ and proceed. If CDPATH is null, it will test ./ to see if it points to a directory (and it will, typically, because this is your pwd).If the -P option is in effect, proceed to step 10. If curpath does not begin with a <slash> character, set curpath to the string formed by the concatenation of the value of PWD, a <slash> character if the value of PWD did not end with a <slash> character, and curpath.
PWD, as it will be $(pwd)/.PWD (or possibly PWD/./ to the same effect)The curpath value shall then be converted to canonical form as follows, considering each component from beginning to end, in sequence:
<slash> characters that separate them from the next component shall be deleted.<slash> characters separating the preceding component from dot-dot, dot-dot, and all characters separating dot-dot from the following component (if any) shall be deleted.<slash> characters that are not also leading characters, replacing multiple non-leading consecutive characters with a single <slash>, and replacing three or more leading <slash> characters with a single <slash>. If, as a result of this canonicalization, the curpath variable is null, no further steps shall be taken.
<slash> added if it does not already have one, is an initial substring of curpath. Whether or not it is considered possible under other circumstances is unspecified. Implementations may also apply this conversion if curpath is not longer than {PATH_MAX} bytes or the directory operand was longer than {PATH_MAX} bytes.
So in essence, a command of cd '' by the standard should cd to the first existing component of CDPATH, if it is set, or to the current directory otherwise. If cd -P '' is used, it will also remove symlinks from the path. In this way, chdir should only be called with an empty string if CDPATH is non-null, but none of its components exist, and cd -P '' is called, as that will pass through step 5, set the curpath to an empty string in step 6, then jump from step 7 to step 10. I don't see any other way that chdir would be called with an empty string, unless a bad implementation takes step 9 too literally and sets curpath to an empty string following the last sentence. ksh93 on Linux and /bin/sh on AIX are nonconformant by these rules. In this way, I'd be careful about using a cd to a path that might evaluate zero-length, as a CDPATH being set can weirdly affect what you're trying to do (though CDPATH has unexpected and confusing behavior anyway, and should not be used in most cases).
Check section 4, Shell & Utilities of the IEEE Std 1003.1™ or Open Group Base Specification.
This contains a separate page for cd, which says:
The cd utility shall then perform actions equivalent to the chdir() function called with curpath as the path argument. If these actions fail for any reason, the cd utility shall display an appropriate error message and the remainder of this step shall not be executed.
This would suggest that the ksh93 that fails on cd "" is actually working according to spec. This is what I see on Ubuntu 14.04, ksh Version AJM 93u+ 2012-08-01.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With