Strange behaviour of the "cd" command

Joel Rees joel.rees at gmail.com
Wed Jul 12 21:59:58 UTC 2017


On Wed, Jul 12, 2017 at 9:04 PM, Karl Auer <kauer at biplane.com.au> wrote:
> On Wed, 2017-07-12 at 12:09 +0200, Dr Rainer Woitok wrote:
>> By pure chance  I meanwhile happened to find a workaround.  Slightly
>> changing your last two commands above yields
>>
>>    $ cd ../..//.three
>>    $ pwd
>>    /tmp/.three
>
> I'll see that and raise you these (skip to the last eight lines of this
> sequence for the finale(s)):
>
> $ mkdir dottest
> $ cd dottest
> $ mkdir .one
> $ cd .one
> $ pwd
> /home/kauer/temp/dottest/.one
> $ mkdir .two
> $ cd .two
> $ pwd
> /home/kauer/temp/dottest/.one/.two
> $ mkdir .three
> $ cd .three
> $ pwd
> /home/kauer/temp/dottest/.one/.two/.three
> $ touch ../../../.one/fred
> $ ls ../../../.one/fred
> ../../../.one/fred
> $ ls -la ..
> total 12
> drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 .
> drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 ..
> drwxrwxr-x 2 kauer kauer 4096 Jul 12 21:24 .three
> $ ls -la ../..
> total 12
> drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 .
> drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 ..
> drwxrwxr-x 3 kauer kauer 4096 Jul 12 21:24 .two
> $ ls -la ../../..
> total 12
> drwxrwxr-x  3 kauer kauer 4096 Jul 12 21:24 .
> drwxr-xr-x 22 kauer kauer 4096 Jul 12 21:24 ..
> drwxrwxr-x  3 kauer kauer 4096 Jul 12 21:24 .one
> $ cd ../../../.one
> ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
> $ cd ../../../\.one
> ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
> $ cd ../../../.one
> ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
> $ cd ../../..//.one
> $ pwd
> /home/kauer/temp/dottest/.one
> $ cd .two/.three
> $ pwd
> /home/kauer/temp/dottest/.one/.two/.three
> $ cd "../../../.one"
> ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
> $ cd '../../../.one'
> ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
> $ cd ../../../..one
> ksh: cd: /home/kauer/temp/dottest/one: [No such file or directory]
> $ cd ../../../...one
> $ pwd
> /home/kauer/temp/dottest/.one
> $ cd .two
> $ cd .three
> $ cd /home/kauer/temp/dottest/.one
> $ pwd
> /home/kauer/temp/dottest/.one
>
> So a double slash stops the dot being vanished by cd, starting at the
> root does too, and three dots will be interpreted as one dot, but NOT,
> further experimentation reveals, in all situations.
>
> This is definitely violating the Principle of Least Astonishment in a
> big way. I wonder if in the Korn Shell World (a strange and wondrous
> place, little visited by me) there is a Grand Unifying Theory of Dots
> that makes this all make sense.
>
> This is from the ksh man page under "File Name Generation":
>
>    If FIGNORE is set, then each file name component
>    that matches the pattern defined by the value of
>    FIGNORE is ignored when generating the matching
>    filenames. The names . and .. are also ignored.
>    If FIGNORE is not set, the character . at the
>    start of each file name component will be
>    ignored unless the first character of the
>    pattern corresponding to this component is the
>    character . itself.
>
> It seems as if this could if we squint a lot) sorta kinda explain only
> the dots in the last component of a path (the "file name") are ignored,
> and why "..." is treated as ".", but it's still very weird.
>
> Other areas of the man page indicate the ".." is special in some other
> contexts too. Maybe those contexts are getting confused by cd?
>
> This is interesting too:
>
> $ x='cd ../../../.one'
> $ echo $x
> cd ../../../.one
> $ pwd
> /home/kauer/temp/dottest/.one/.two/.three
> $ cd $x
> ksh: cd: bad substitution
> $ cd "$x"
> ksh: cd: cd ../../../.one: [No such file or directory]

I have vague memories from college (thirty+ years ago), of one of
the popular shells having some odd behavior with the .. notations,
like ... being an abbreviation of ../.. or something.

Also, I have vague memories of dot itself being one of the pattern
matching characters in one of the shells, which induced ambiguities
that could be used for tricky stuff -- a definite misfeature.

Ultimately, some professors did not recommend csh for scripts. Too
many things you couldn't predict reliably.

ksh is a descendant of csh.

-- 
Joel Rees

One of these days I'll get someone to pay me
to design a language that combines the best of Forth and C.
Then I'll be able to leap wide instruction sets with a single #ifdef,
run faster than a speeding infinite loop with a #define,
and stop all integer size bugs with my bare cast.
http://defining-computers.blogspot.com/2017/06/reinventing-computers.html

More of my delusions:
http://reiisi.blogspot.com/2017/05/do-not-pay-modern-danegeld-ransomware.html
http://reiisi.blogspot.jp/p/novels-i-am-writing.html




More information about the ubuntu-users mailing list