Alexandre Halm
Alexandre Halm

Reputation: 989

File is both a regular file and a symlink

I have three files, one a real ("regular") file and two chained symlinks:

-rw-r--r--   1 user  staff       24  4 Nov 18:23 A
lrwxr-xr-x   1 user  staff        1 25 Oct 17:43 B -> A
lrwxr-xr-x   1 user  staff        1  4 Nov 16:54 C -> B

Now, using zsh conditional expressions ([[), I test whether B (or C) is a regular file or a symlink, and zsh says yes to both ...:

18:31 : [[ -f B ]] && echo "True"
True
18:31 : [[ -h B ]] && echo "True"
True

Isn't a symlink supposed not to be a regular file? zsh documentation of conditional expressions (here) doesn't give full details on how different cases work.

zsh --version
zsh 5.0.5 (x86_64-apple-darwin14.0)

edit

After more digging, I found this Q&A on Unix.SE : https://unix.stackexchange.com/questions/22009/distinguishing-a-regular-file-from-a-symlink. It's about bash but it looks like it also applies to zsh.

Upvotes: 2

Views: 142

Answers (1)

Kevin
Kevin

Reputation: 30161

zsh is almost certainly using the S_ISREG() and S_ISLNK() macros from lstat(2). Since S_ISLNK() is younger than S_ISREG(), I imagine symlinks were historically treated like regular files here (since they aren't directories, block/character devices, etc.), and the behavior was kept for backwards compatibility. I wasn't able to find any direct evidence of this, however.

Upvotes: 1

Related Questions