bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does [ -f FILE ] have a bug on testing a symlink ?


From: Chet Ramey
Subject: Re: Does [ -f FILE ] have a bug on testing a symlink ?
Date: Mon, 09 Feb 2015 16:45:33 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.4.0

On 2/9/15 4:00 PM, Cheng Rk wrote:
> 
> 
> To bug-bash@gnu.org:
> 
> 
> According this documentation `help test`, I am expecting it should return 
> false on anything other than a regular file,
> 
> -f FILE        True if file exists and is a regular file.
> 
> 
> but why it returned true on a symlink to a regular file?
> 
> $ [ -f tmp/sym-link ] && echo true
> true

This is fundamental to how symbolic links work.  Unless you test specially
for a symlink and use system calls like lstat and readlink to obtain
values, system calls that operate on filenames follow symbolic links
(open, stat, etc.).

The bash man page notes this:

"Unless otherwise specified, primaries that operate on files
follow symbolic links and operate on the target of the link, rather than
the link itself."

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]