On 1/27/24 10:30 PM, j...@jwo.cz wrote:
Hello,(If you don't want to read through this stuff, see the end or grep for word ``opinion''.) I started exploring details of file handling on Linux and similar systems and wondered what ``cd ""'' will do.
It's unspecified in the current POSIX edition. Using the POSIX algorithm, the results depend on $CDPATH and $PWD.
And now the behavior of the Bash. I tried these versions: GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) GNU bash, version 5.1.8(1)-release (x86_64-unknown-freebsd13.0) GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu) GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu) GNU bash, version 5.2.26(0)-release (amd64-portbld-freebsd13.2) The behavior is the same as for the zsh. It simply leaves the working directory unchanged.
That's the result of this step in the POSIX algorithm (`curpath' is the empty string argument):"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."
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cd.html#tag_20_14
POSIX Programmer's Manual (cd(1p) manpage) says this:
I don't know what this is.
[9 unrelated special cases]
If that page is derived from the actual standard, the previous 9 steps are decidedly not `unrelated special cases.'
10. 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. [...] So, it should do the same thing a chdir("").
It should not. It could do that if you used `cd -P ""' with CDPATH unset, but you didn't.
My opinion on this is that the ``cd ""'' command should fail with an error in this case. The zsh is the only POSIX-compatible or POSIX-shell-like shell that was available on machines to which I have access that exhibits this behavior.
The next edition of POSIX agrees with you.
I propose to make Bash report an error when the user attempts to cd into empty string, possibly with an shopt option that allows changing behaviour of the cd back to the current one -- silently ignoring the command.
In POSIX mode, definitely, since POSIX will require it. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRU c...@case.edu http://tiswww.cwru.edu/~chet/
OpenPGP_signature.asc
Description: OpenPGP digital signature