0

My system is macOS 13.2.1, the shell is oh-my-zsh (same behavior on bash). I found a weird behavior on cd command. It changes the current directory to a wrong one. This is not caused by the soft link, because there is no directory in the right path.

!w /Library/TeX/texbin ........................................................................................................... 14:56:24
> cd ../../texmf-dist

!w /usr/local/texlive/2021/texmf-dist ............................................................................................ 14:56:40 > cd -
/Library/TeX/texbin

!w /Library/TeX/texbin ........................................................................................................... 14:56:42 > cd ../../

!w /Library ...................................................................................................................... 14:56:51 > cd texmf-dist
cd: no such file or directory: texmf-dist

Kusalananda
  • 333,661

1 Answers1

1

You mentioned /Library/TeX is a symbolic link to /usr/local/texlive/2021/universal-darwin/, and that causes the ambiguity.

When you're in /Library/TeX/texbin (really /usr/local/texlive/2021/universal-darwin/texbin) and run cd ../.., the shell looks to see if /Library/texmf-dist exists, that is, it remembers the path with the link you used to go there, and removes one path element for each .., in effect undoing the step taken through the link. You end up in /Library. And /Library/texmf-dist doesn't exist so the final cd doesn't work.

Similarly with cd ../../texmf-dist it would try to go to /Library/texmf-dist but that doesn't exist. But here, it checks if that path makes sense in the actual location you're in, without remembering the symlink. It does, and you end up in /usr/local/texlive/2021/texmf-dist. Note how the path changed completely as a consequence of the shell having to forget the path via the symlink.

If you go back to /Library/TeX/texbin and run cd -P ../.. instead, it uses the "physical" path, again forgetting the symbolic link, and you end up in /usr/local/texlive/2021, where cd texmf-dist would again work normally.

The structure looks like this, and the difference comes from which path up is chosen when going to .. from universal-darwin (I assume none of the other dirs are symlinks):

/Library -> Tex - - - -(symlink)- - +
                                    v
/usr -> local -> texlive -> 2021 -> universal-darwin -> texbin
                              |
                              +---> texfmt-dist

The way the shell remembers the symlink you used is a usability feature, in many cases it means you don't need to remember where something actually is, when you can just make a symlink with a nicer name and go through that. It's easier to remember ~/thingy rather than /net/fileserver1/disk02/dir/thingy or whatever.

But it means the directory tree isn't a tree any more, and "up" turns ambiguous.

Hence, it can break down when going up "past" the symlink target. In particular programs started from the shell won't know what the shell remembers about the symlinks and they'll use the "physical" parent directory, always. E.g. in your case, with /Library/TeX being a symlink, running ls .. there should show a different listing than cd ..; ls.

ilkkachu
  • 138,973