if [ -h "demo" ]; then
echo "-h: true"
fi
if [ -L "demo" ]; then
echo "-L: true"
fi
I found that both "-h" and "-L" can be used to determine whether the specified file is a link. What is the difference between them?
They both exist for legacy reasons, to be compatible between different versions of Unix.
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.
Some versions of Solaris test
only support -h
, and (back in 2003) some software has switched to -h
for compatibility reasons.
So it's probably best to use -h
Source: Difference between test -h and test -L, answer by Brian Campbell
Looking at an AlmaLinux 8.7 installation:
man test
indicates the two options are the same:
-h FILE
FILE exists and is a symbolic link (same as -L)
<snip>
-L FILE
FILE exists and is a symbolic link (same as -h)
man bash
just reports the same help text for both options, without mentioning they are the same:
-h file
True if file exists and is a symbolic link.
<snip>
-L file
True if file exists and is a symbolic link.
From the above man text, it isn't clear which one will have best compatibility with other distributions.
As @DavidPostill has already noted, research is required to identity which option has best compatibility.
[
is a command that requires its last argument to be]
, so it looks like some special syntax; but it's still a command likels
orcat
.[ … ]
is equivalent totest …
wheretest
is a command that doesn't look like some special syntax.[
andtest
are builtins in Bash, but they behave like commands nevertheless. You can use[
as a standalone executable (it may be/usr/bin/[
). My point is-h
and-L
in question are not directly related to Bash. They belong to the specification of[
ortest
.[
in Bash would get any Bash-specific differences if there were any...echo
is also a builtin in Bash, but I would expect "what is-e
in Bash?" to refer tobash -e
, not toecho -e
. And there is[ -e
; andecho -n
and[ -n
. By your reasoning, "what is the difference between-e
and-n
in bash?" may refer to theecho
builtin or to the[
builtin, or to any builtin. I reject this ambiguity. The title was wrong.