NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: standards/59565: sh(1): cd "" must fail



    Date:        Wed, 30 Jul 2025 17:40:01 +0000 (UTC)
    From:        Thomas Klausner <wiz%NetBSD.org@localhost>
    Message-ID:  <20250730174001.2B56F1A923F%mollari.NetBSD.org@localhost>

  | I think one of the reasons for this is to avoid hidden errors with code
  | such as
  |
  | foo=$(some shell script giving no output)
  | cd "$foo"

The common example of that would be something like:

	foo=$(mktemp -d /tpm/foo-dir-XXXXX)
	cd "${foo}"

  | >Fix:
  | Yes, perhaps after discussion.

FWIW the fix is absurdly trivial - the code that makes this work is
a very special case in the shell builtin cd implementation - that case
just needs a minor change (FreeBSD simply deleted the special case code
earlier this month, but I doubt that is actually the correct solution,
that is, I doubt they properly tested their change in all cases, but until
I start changing code, I'm not certain.)    bash also removed the code it
had to do this in the most recent release (5.3).

The right thing to do is probably to do what POSIX say should be done in
this case - but we have had this special case code in our cd command now
for approaching 30 years (it was added in Oct 1996 - in amongst a whole
set of patches, copied from the FreeBSD shell of the time).   Hence some
discussion of whether or not we should follow POSIX or history is perhaps
warranted, before just changing it.

We also need to consider (which FreeBSD and bash do not, but some ksh
versions might) what we do with the 2 arg version of "cd" in a case
like this:

	cd /tmp
	cd /tmp ''

The 2nd cd there looks for the string "/tmp" in $PWD (which is /tmp because
of the first cd command) and replaces that string (if found, if not it is
an error and nothing happens) with the 2nd arg - in this case producing "".

Because of the special case code under consideration here, that turns into
"cd ." and the directory doesn't change.   If we remove the special case,
this will become an error (if the 1st arg of the 2 in the 2 arg case is $PWD
and the 2nd arg is '').    That's probably OK too, but it does deserve
separate consideration (we could allow this one to remain as it is, while
just changing the 'cd ""' case - the 2 arg cd command is a POSIX extension).

kre



Home | Main Index | Thread Index | Old Index