NetBSD-Users archive

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

Re: 'cd' if HOME is unset



    Date:        Sat, 24 Dec 2022 22:32:22 -0500
    From:        Jan Schaumann <jschauma%netmeister.org@localhost>
    Message-ID:  <Y6fERit3iasKXGB8%netmeister.org@localhost>

  | I happily admit that it's a rare edge case. I simply
  | find it surprising that 'cd' gives up if HOME is
  | unset.  Seems unintuitive to me.

It is how it is defined to work, and always has been.
Only dash and zsh seem to handle that case, no other
shells bother.   (I am a little surprised that dash does,
their general philosophy tends towards minimalist implementation,
with almost nothing that isn't required).

Better is just to always have HOME set.

For /bin/sh ~ works without HOME, so you could define

	cd() {
		case "$#" in
		0)	set -- ~ ;;
		esac
		command cd "$@"
	}

if you wanted to.   But that's not required to work either (I'm
not surprised that dash doesn't expand ~ when HOME is not set,
that's more in line with what I'd expect ... though tilde expansion
working is, in general, more useful, than cd with no args when
HOME is unset, so if a shell was to do just one, I'd generally do
it the way we do (as does bash)).

kre

ps: I am a little surprised that csh acts this way though, it
started from the Thompson sh (ie: pre 7th edition) and back then
there was no environment, and while csh had vars (incl home)
it couldn't have cd depend upon what was in the environment, and
had to either use the passwd db, either for "cd" or to init "home"
or both.   I guess that has been changed since.




Home | Main Index | Thread Index | Old Index