zsh-workers
 help / color / mirror / code / Atom feed
* BUG: cd '' doesn't produce an error
@ 2005-12-21  2:03 Ulrik Haugen
  2005-12-21 10:21 ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Ulrik Haugen @ 2005-12-21  2:03 UTC (permalink / raw)
  To: zsh-workers

Hello!

I recently discovered that zsh (versions 4.2.5 and 4.3.0-dev-1)
doesn't produce an error when i attempt to "cd ''" (in version 3.0.8 I
get the error i expected). Please let me know if you'd like me to test
any other versions.

Below is an example that demonstrates the problem, I was expecting an
error message and exit code from "cd ''" similar to what I get from
"cd flupp".

% mkdir /tmp/test
% cd /tmp/test
% pwd       
/tmp/test
% cd flupp
cd: no such file or directory: flupp
% echo $?
1
% pwd       
/tmp/test
% cd ''
% echo $?
0
% pwd
/tmp/test


Is this the bug I see or is it intended to be this way?

Some other shells I've tested:
bash 3.00.16(1)-release:   no error from cd ''
/bin/sh on SunOS 5.10:     expected behaviour
tcsh 6.14.00:              expected behaviour


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2005-12-21  2:03 BUG: cd '' doesn't produce an error Ulrik Haugen
@ 2005-12-21 10:21 ` Peter Stephenson
  2005-12-30 12:52   ` Ulrik Haugen
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2005-12-21 10:21 UTC (permalink / raw)
  To: zsh-workers

Ulrik Haugen <qha@lysator.liu.se> wrote:
> I recently discovered that zsh (versions 4.2.5 and 4.3.0-dev-1)
> doesn't produce an error when i attempt to "cd ''" (in version 3.0.8 I
> get the error i expected). Please let me know if you'd like me to test
> any other versions.

It's certainly strange, although it's not wholly illogical with $cdpath: as
the empty string is not an absolute directory nor begins with . or .., the
shell looks through $cdpath for the first directory for which the
concatenation with the empty string exists, which is usually just the first
directory in $cdpath.  This is almost useful...  It's easy to add an error
but I'm not quite sure at what stage the behaviour actually is an error in
this case: it's a correct, if unexpected, interpretation of the cdpath
rules.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2005-12-21 10:21 ` Peter Stephenson
@ 2005-12-30 12:52   ` Ulrik Haugen
  2005-12-30 16:28     ` Wayne Davison
  2006-01-04 11:41     ` Peter Stephenson
  0 siblings, 2 replies; 11+ messages in thread
From: Ulrik Haugen @ 2005-12-30 12:52 UTC (permalink / raw)
  To: zsh-workers

Hello!

Peter Stephenson <pws@csr.com> wrote:

> Ulrik Haugen <qha@lysator.liu.se> wrote:
>> I recently discovered that zsh (versions 4.2.5 and 4.3.0-dev-1)
>> doesn't produce an error when i attempt to "cd ''" (in version 3.0.8 I
>> get the error i expected). Please let me know if you'd like me to test
>> any other versions.

> It's certainly strange, although it's not wholly illogical with $cdpath: as
> the empty string is not an absolute directory nor begins with . or .., the
> shell looks through $cdpath for the first directory for which the
> concatenation with the empty string exists, which is usually just the first
> directory in $cdpath.  This is almost useful...  It's easy to add an error
> but I'm not quite sure at what stage the behaviour actually is an error in
> this case: it's a correct, if unexpected, interpretation of the cdpath
> rules.

Sorry for taking so long to reply, but I haven't had the time to look
into properly this until now.

I'm still not quite satisfied with this behaviour for a couple of
reasons:

In my tests I had not set cdpath, so I don't see why the shell should
go through any cdpath rules.


If I try
% cd /tmp; CDPATH=/usr cd ''
I still wind up in /tmp, so it doesn't seem to have that much to do
with cdpath to me.


If I had ended ended up in /usr, I'm not really sure I'd like that
either (although someone who uses cdpath is probably a better judge).
My intuitive expectation was that cd would successively cd to each
component of cdpath, try to cd to the argument given and be satisfied
at the first successful cd (though obviously just appending "/$arg" to
each component in cdpath and successively trying to cd to that is
cheaper).





The reason I take the time to argue about this is that I often want to
determine the absolute path my scripts are called from in this
fashion:

scriptdir=`dirname "$0"`
scriptdir=`cd "$scriptdir"&& pwd`

More than once I've decided I'm not happy with whatever variable name
has had the role of scriptdir and only changed the occurences at the
beginnings of lines. Right now zsh silently ignores my error and I'll
get the callers present working directory in the whatever new name
I've chosen for scriptdir. Admittedly I'd get a faulty value if zsh
complained to, but at least I'd get some indication of my mistake.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2005-12-30 12:52   ` Ulrik Haugen
@ 2005-12-30 16:28     ` Wayne Davison
  2006-01-04 17:32       ` Wayne Davison
  2006-01-04 19:48       ` Ulrik Haugen
  2006-01-04 11:41     ` Peter Stephenson
  1 sibling, 2 replies; 11+ messages in thread
From: Wayne Davison @ 2005-12-30 16:28 UTC (permalink / raw)
  To: Ulrik Haugen; +Cc: zsh-workers

On Fri, Dec 30, 2005 at 01:52:42PM +0100, Ulrik Haugen wrote:
> Right now zsh silently ignores my error

It would be better to address this aspect of the problem rather than
trying to change the cd command.  There are a couple ways to deal with
this:

1.  Specify "setopt no_unset err_exit" in the script.  This will ensure
    that no unset variable will ever be silently expanded into an empty
    string unless you explicitly request it via ${FOO:-} .

2.  Use a single must-be-defined check via ${FOO:?}, like this:

    scriptdir=`dirname "$0"`
    scriptdir=`cd "${scriptdir:?}"&& pwd`

    That will output a "parameter not set" error for a single variable
    instead of all variables.  The documentation claims that this error
    should cause the shell to exit, but in my testing it did not do so
    without the err_exit option also being set.

..wayne..


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2005-12-30 12:52   ` Ulrik Haugen
  2005-12-30 16:28     ` Wayne Davison
@ 2006-01-04 11:41     ` Peter Stephenson
  2006-01-04 13:06       ` Peter Stephenson
                         ` (2 more replies)
  1 sibling, 3 replies; 11+ messages in thread
From: Peter Stephenson @ 2006-01-04 11:41 UTC (permalink / raw)
  To: zsh-workers

Ulrik Haugen wrote:
> If I try
> % cd /tmp; CDPATH=/usr cd ''
> I still wind up in /tmp, so it doesn't seem to have that much to do
> with cdpath to me.

That's because of the special handling of ".": if it doesn't occur in
cdpath, it's assumed to occur first.

That's long-standing behaviour, although I think the documentation (for
cd) is a bit screwy...

          Otherwise [if the argument is not -], if a directory named arg
          is not found in the current directory and arg does not begin
          with a slash, search each component of the  shell  parameter
          cdpath.

This isn't actually quite right, since if . is in $cdpath but not at
the start it doesn't search . straight away.  So at least the
description needs improving (this isn't special to a null string).

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Your mail client is unable to display the latest news from CSR. To access our news copy this link into a web browser:  http://www.csr.com/email_sig.html


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2006-01-04 11:41     ` Peter Stephenson
@ 2006-01-04 13:06       ` Peter Stephenson
  2006-01-04 17:48       ` Peter Stephenson
  2006-01-04 22:30       ` Ulrik Haugen
  2 siblings, 0 replies; 11+ messages in thread
From: Peter Stephenson @ 2006-01-04 13:06 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson wrote:
> Your mail client is unable to display the latest news from CSR. To access our
>  news copy this link into a web browser:  http://www.csr.com/email_sig.html

This is, of course, nonsense and you don't want to see the news from
CSR anyway.  This addition is controversial anyway.  Please just ignore
it for now...

pws


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2005-12-30 16:28     ` Wayne Davison
@ 2006-01-04 17:32       ` Wayne Davison
  2006-01-04 19:48       ` Ulrik Haugen
  1 sibling, 0 replies; 11+ messages in thread
From: Wayne Davison @ 2006-01-04 17:32 UTC (permalink / raw)
  To: Ulrik Haugen; +Cc: zsh-workers

On Fri, Dec 30, 2005 at 08:28:41AM -0800, Wayne Davison wrote:
> The documentation claims that this error should cause the shell to
> exit, but in my testing it did not do so without the err_exit option
> also being set.

It was silly of me to not notice that the reason for this was that the
error was occurring in a subshell (inside the backticks), so that
subshell was exiting, not the main shell.

..wayne..


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2006-01-04 11:41     ` Peter Stephenson
  2006-01-04 13:06       ` Peter Stephenson
@ 2006-01-04 17:48       ` Peter Stephenson
  2006-01-04 22:30       ` Ulrik Haugen
  2 siblings, 0 replies; 11+ messages in thread
From: Peter Stephenson @ 2006-01-04 17:48 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson <pws@csr.com> wrote:
>           Otherwise [if the argument is not -], if a directory named arg
>           is not found in the current directory and arg does not begin
>           with a slash, search each component of the  shell  parameter
>           cdpath.
> 
> This isn't actually quite right, since if . is in $cdpath but not at
> the start it doesn't search . straight away.  So at least the
> description needs improving (this isn't special to a null string).

Here's a first attempt...

Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.81
diff -u -r1.81 builtins.yo
--- Doc/Zsh/builtins.yo	7 Nov 2005 09:37:34 -0000	1.81
+++ Doc/Zsh/builtins.yo	4 Jan 2006 17:47:23 -0000
@@ -160,12 +160,22 @@
 current directory to var(arg), or to the value of tt($HOME) if
 var(arg) is not specified.  If var(arg) is `tt(-)', change to the
 value of tt($OLDPWD), the previous directory.
-Otherwise, if a directory named var(arg) is not found in the current
-directory and var(arg) does not begin with a slash, search each
-component of the shell parameter tt(cdpath).  If no directory is found
-and the option tt(CDABLE_VARS) is set, and a parameter named var(arg)
-exists whose value begins with a slash, treat its value as the
-directory.  In that case, the parameter is added to the named
+
+Otherwise, if var(arg) begins with a slash, attempt to change to the
+director given by var(arg).
+
+If var(arg) does not begin with a slash, the behaviour depends on whether
+the current directory `tt(.)' occurs in the list of directories contained
+in the shell parameter tt(cdpath).  If it does not, first attempt to change
+to the directory var(arg) under the current directory, and if that fails
+but tt(cdpath) is set and contains at least one element attempt to change
+to the directory var(arg) under each component of tt(cdpath) in turn until
+successful.  If `tt(.)' occurs in tt(cdpath), then tt(cdpath) is searched
+strictly in order so that `tt(.)' is only tried at the appropriate point.
+
+If no directory is found, the option tt(CDABLE_VARS) is set, and a
+parameter named var(arg) exists whose value begins with a slash, treat its
+value as the directory.  In that case, the parameter is added to the named
 directory hash table.
 
 The second form of tt(cd) substitutes the string var(new)

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Your mail client is unable to display the latest news from CSR. To access our news copy this link into a web browser:  http://www.csr.com/email_sig.html


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2005-12-30 16:28     ` Wayne Davison
  2006-01-04 17:32       ` Wayne Davison
@ 2006-01-04 19:48       ` Ulrik Haugen
  1 sibling, 0 replies; 11+ messages in thread
From: Ulrik Haugen @ 2006-01-04 19:48 UTC (permalink / raw)
  To: zsh-workers

Wayne Davison <wayned@users.sourceforge.net> wrote:
> 1.  Specify "setopt no_unset err_exit" in the script.  This will ensure
>     that no unset variable will ever be silently expanded into an empty
>     string unless you explicitly request it via ${FOO:-} .

These sound like very good ideas, in fact no_unset went into my .zshrc
right away. I'll try to remember these when i write zsh scripts in the
future, it shouldn't be much worse than remembering to use strict and
warnings in perl.

I'm not sure I'll be as likely to remember it when I'm debugging a sh
script and just switch to zsh to see if I've accidentally used some
zshism though, especially since I'll usually want to switch back
afterwards. :|


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2006-01-04 11:41     ` Peter Stephenson
  2006-01-04 13:06       ` Peter Stephenson
  2006-01-04 17:48       ` Peter Stephenson
@ 2006-01-04 22:30       ` Ulrik Haugen
  2006-01-05 12:29         ` Peter Stephenson
  2 siblings, 1 reply; 11+ messages in thread
From: Ulrik Haugen @ 2006-01-04 22:30 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson <pws@csr.com> wrote:
> That's because of the special handling of ".": if it doesn't occur in
> cdpath, it's assumed to occur first.

> That's long-standing behaviour, although I think the documentation (for
> cd) is a bit screwy...

Ok, after reading your documentation changes and tinkering a bit my
self I think I've gotten a grasp on cdpath.

It seems to me that having another directory in cdpath before . would
be quite unusual, and that was the one case when cd to the empty
string was even remotely useful. I don't think that motivates not
complaining about an argument that you can't stat(2), but I can't
think of anything but subjective reasons any more though. :|


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: BUG: cd '' doesn't produce an error
  2006-01-04 22:30       ` Ulrik Haugen
@ 2006-01-05 12:29         ` Peter Stephenson
  0 siblings, 0 replies; 11+ messages in thread
From: Peter Stephenson @ 2006-01-05 12:29 UTC (permalink / raw)
  To: zsh-workers

Ulrik Haugen <qha@lysator.liu.se> wrote:
> It seems to me that having another directory in cdpath before . would
> be quite unusual, and that was the one case when cd to the empty
> string was even remotely useful. I don't think that motivates not
> complaining about an argument that you can't stat(2), but I can't
> think of anything but subjective reasons any more though. :|

Yes, it's not particularly useful, it's just not clear that it's actually a
bug.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Your mail client is unable to display the latest news from CSR. To access our news copy this link into a web browser:  http://www.csr.com/email_sig.html


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2006-01-05 12:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-21  2:03 BUG: cd '' doesn't produce an error Ulrik Haugen
2005-12-21 10:21 ` Peter Stephenson
2005-12-30 12:52   ` Ulrik Haugen
2005-12-30 16:28     ` Wayne Davison
2006-01-04 17:32       ` Wayne Davison
2006-01-04 19:48       ` Ulrik Haugen
2006-01-04 11:41     ` Peter Stephenson
2006-01-04 13:06       ` Peter Stephenson
2006-01-04 17:48       ` Peter Stephenson
2006-01-04 22:30       ` Ulrik Haugen
2006-01-05 12:29         ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).