views:

52

answers:

4

What is the difference between test -L filename and test -h filename in ksh shell. From the man page, both were used to identify a symbolic link, but I want to know the exact difference.

Here is the description from the man page.

 -h file                 True if file exists and  is  a  sym-
                         bolic link.
 -L file                 True if file exists and  is  a  sym-
                         bolic link.
+3  A: 

It appears that they both exist for legacy reasons, to be compatible between different versions of Unix. You should be able to use either one, as they do the exact same thing, but be aware that if the system you are running on is not compliant with the latest standards, it may be missing one or the other.

Both forms are present in the Single Unix Specification version 3/POSIX 2004, with no caveats:

-h  pathname
True if pathname resolves to a file that exists and is a symbolic link. False if pathname cannot be resolved, or if pathname resolves to a file that exists but is not a symbolic link. If the final component of pathname is a symlink, that symlink is not followed.
-L  pathname
True if pathname resolves to a file that exists and is a symbolic link. False if pathname cannot be resolved, or if pathname resolves to a file that exists but is not a symbolic link. If the final component of pathname is a symlink, that symlink is not followed.

According to the test(1) man page on Mac OS X and FreeBSD (note that this warning may be outdated; it first appeared in NetBSD in 1996):

     -h file       True if file exists and is a symbolic link.  This operator
                   is retained for compatibility with previous versions of
                   this program. Do not rely on its existence; use -L instead.

And apparently, some versions of Solaris test only support -h, and (back in 2003) some software has switched to -h for compatibility reasons, so -h may actually be your best bet.

Brian Campbell
`Do not rely on its existence; use -L instead` Is it written on the man page
Sachin Chourasiya
Yes, I quoted straight from the man page in my answer in order to point that out.
Brian Campbell
Note that after some further research, that line in the `man` page is quite old, and may be outdated.
Brian Campbell
Thanks Brain, I got the answe. I am using -L for identifying the symbolic link.
Sachin Chourasiya
A: 

Fedora's man page says

   -h FILE
          FILE exists and is a symbolic link (same as -L)
wallyk
+2  A: 

There is no difference, they are exactly the same. They probably exist to unify different test implementations pre-POSIX.

Cirno de Bergerac
+2  A: 

The source code for ksh93, in file bltins/test.c, shows that these two options are treated exactly the same, except for the author's hopes for the future:

        case 'L':
        case 'h': /* undocumented, and hopefully will disappear */
            if(*arg==0 || arg[strlen(arg)-1]=='/' || lstat(arg,&statb)<0)
                    return(0);
            return(S_ISLNK(statb.st_mode));

From this I conclude that they behave exactly the same, but that -h is a legacy option and may one day disappear :-)

Norman Ramsey
I appreciate the way of answering my question.
Sachin Chourasiya