You are not logged in.

#1 2025-03-02 02:26:58

solskog
Member
Registered: 2020-09-05
Posts: 434

[Solved] Bash builtin test

GNU bash, version 5.2.37(1)-release (x86_64-pc-linux-gnu)
$ mkdir dir
$ ln -sf dir link2dir
$ builtin test -h link2dir && echo Link
$ Link
$ builtin test -d link2dir && echo Directory
$ Directory

I understand the "builtin test" dereferencing to the target "dir".  Can I test a symbol link without dereferencing using pure bash?
I ended up using "realpath" instead.

Last edited by solskog (2025-03-02 04:29:38)

Offline

#2 2025-03-02 09:49:53

seth
Member
Registered: 2012-09-03
Posts: 61,426

Re: [Solved] Bash builtin test

Sanity check, what's the problem here?
The file exists and is a link AND is a directory, this is the same behavior on /usr/bin/test and bash/zsh built-ins

man test wrote:

Except for -h and -L, all FILE-related tests dereference symbolic links.

what makes sense since any other information about symlinks is close to pointless (maybe there's a case for checking their timewstamps, "find" is your friend here, though - but not pure bash)

Offline

#3 2025-03-02 13:04:02

solskog
Member
Registered: 2020-09-05
Posts: 434

Re: [Solved] Bash builtin test

except for -h and -L, all FILE-related tests dereference symbolic links.

You are right, I wasn't pay attention. I somehow answered my own question.

Last edited by solskog (2025-03-02 13:35:22)

Offline

Board footer

Powered by FluxBB