Bug 1164895 - cd does not switch to the parent directory
Summary: cd does not switch to the parent directory
Status: RESOLVED WONTFIX
Alias: None
Product: openSUSE Distribution
Classification: openSUSE
Component: Documentation (show other bugs)
Version: Leap 15.1
Hardware: x86-64 Windows 10
: P5 - None : Normal with 5 votes (vote)
Target Milestone: ---
Assignee: Frank Sundermeyer
QA Contact: Frank Sundermeyer
URL: https://doc.opensuse.org/documentatio...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-02-25 20:22 UTC by Christopher Yeleighton
Modified: 2020-02-26 19:12 UTC (History)
1 user (show)

See Also:
Found By: Community User
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christopher Yeleighton 2020-02-25 20:22:27 UTC
Contrary to what the documentation says, the command cd does not switch to the parent directory of the current directory.  It switches to the directory referred to by a path obtained by stripping the last element of the current directory path (which is not always the path to the current directory).

Example:
    { cd /proc/1/cwd/..; }

The parent directory of /proc/1/cwd is / — but the command takes me to /proc/1 instead.
Comment 1 Christopher Yeleighton 2020-02-25 20:38:43 UTC
In other words, assuming you are in /proc/1/cwd, the following instructions are not equivalent:

 * { cd ../mnt; }
 * { cd .. && cd mnt; }
Comment 2 Neil Rickert 2020-02-25 23:26:18 UTC
I doubt that this is a real bug.

Two points:

(1)  "cd" is a shell internal command.  So you ought mention which shell you are using.

(2) "/proc" isn't a real file system.  It is a virtual construct.  So what happens in "/proc" is not a good indication of what would happen in a real file system.
Comment 3 Christopher Yeleighton 2020-02-26 07:57:43 UTC
(In reply to Neil Rickert from comment #2)
> I doubt that this is a real bug.

The shell behaves differently than documented, so it is a bug.

> 
> Two points:
> 
> (1)  "cd" is a shell internal command.  So you ought mention which shell you
> are using.
> 

The documentation is about Bash.

> (2) "/proc" isn't a real file system.  It is a virtual construct.  So what
> happens in "/proc" is not a good indication of what would happen in a real
> file system.

Take /var/run for the same effect.
Comment 4 Frank Sundermeyer 2020-02-26 13:48:17 UTC
That sounds a bit like splitting hairs to me, because it depends on the definition of current path. Given the command names that exist to show paths I would argue that current path==working directory which differs from the real path:

> cd /var/run/
> pwd
> /var/run
> realpath .
/run

the result of "cd -P .." is probably the behavior you expect, but IMHO this is not what the majority would expect. I agree with you that the description in our docs is wrong--strictly technically spoken--but it nevertheless describes the expected behavior. Changing/explaining this causes IMHO more confusion than clarity and therefore I would like to leave it as is.
Comment 5 Christopher Yeleighton 2020-02-26 13:53:50 UTC
(In reply to Frank Sundermeyer from comment #4)
> describes the expected behavior. Changing/explaining this causes IMHO more
> confusion than clarity and therefore I would like to leave it as is.

I would change it to "the previous directory in the current path", which is correct and not oh so confusing IMHO.  It works path-wise, not inode-wise.
Comment 6 Neil Rickert 2020-02-26 16:37:36 UTC
I'll note that I normally use "csh" (which is really "tcsh") as my shell.

And "csh" has always behaved this way (even before there was a "tcsh").  That is to say "cd .." prunes the last item from $cwd.  I consider this a feature, not a bug.  The Bourne shell worked the other way.  But "bash" includes some of the nice features from "csh", and I guess this is one of them.

If I start a subshell with the "csh" command, then "cd .." will work as you expect, because $cwd is not passed to the subshell.  However, if I start an internal subshell by using parentheses, as in
 ( subshell commands )
then $cwd is passed.

It's a matter of getting used to your shell features.

With csh:
  cd /var/run
  pwd

returns "/run".  But bash fakes that, too.

Hmm, if I invoke the shell as "/bin/sh" (instead of as "/bin/bash") it still behaves that way.  I suppose that's a "bashism".  I don't know whether that adversely affects any shell scripts.

If there is a bug, it would seem to me that the bug is that even when invoked as "/bin/bash" it behaves the way you describe.
Comment 7 Christopher Yeleighton 2020-02-26 19:12:52 UTC
(In reply to Neil Rickert from comment #6)
> If there is a bug, it would seem to me that the bug is that even when
> invoked as "/bin/bash" it behaves the way you describe.

The bug is in the documentation.