6

According to the man page:

       -h file
              True if file exists and is a symbolic link.
    ...
       -L file
              True if file exists and is a symbolic link.

They both seem to say the exact same thing, but then why two separate flags? Are reasons of historical compatibility? Some subtle difference across versions of bash? Something else?

1 Answer 1

7

There is no difference whatsoever. The POSIX description of both options reads

True if pathname resolves to an existing directory entry for a symbolic link. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a symbolic link. If the final component of pathname is a symbolic link, that symbolic link is not followed.

As you say, the reason to have both is most likely to support the most common choices made by historical implementations of the test and [ utilities.

As for the test and [ built-in utilities of bash, they both functions the same, and have always done. The oldest available bash source in the current Git repository has the same wording for both (release 2.04, from 1991).

In the actual 1991 implementation, there's even a comment:

    case 'L':           /* Same as -h  */
    case 'h':           /* File is a symbolic link? */
4
  • Thank you for citing source. I'm genuinely surprised to see that this was never phased out. I'd be curious about the history of why this was put in the way it was.
    – ffledgling
    Dec 11, 2019 at 21:39
  • @ffledgling Put into bash? Probably because it's in the POSIX standard.
    – Kusalananda
    Dec 12, 2019 at 3:56
  • Put into the POSIX standard in the first place I think
    – ffledgling
    Dec 12, 2019 at 21:59
  • 1
    @ffledgling The POSIX standard does not invent but standardizes existing behavior. When the test utility was standardized, the committee looked at what options etc. were in use at the time and obviously came to the conclusion that they needed to include both -h and -L to not render too much of existing software obsolete.
    – Kusalananda
    Mar 20, 2020 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.