If you want to check whether $path
is a symbolic link whose target is /some/where
, you can use the readlink
utility. It isn't POSIX, but it's available on many systems (GNU/Linux, BusyBox, *BSD, …).
if [ "$(readlink -- "$path")" = /some/where ]; then …
Note that this is an exact text comparison. If the target of the link is /some//where
, or if it's where
and the value of $path
is /some/link
, then the texts won't match.
Many versions of readlink
support the option -f
, which canonicalizes the path by expanding all symbolic links.
Many shells, including dash, ksh, bash and zsh, support the -ef
operator in the test
builtin to test whether two files are the same (hard links to the same file, after following symbolic links). This feature is also widely supported but not POSIX.
if [ "$path" -ef "/some/where" ]; then …
readlink
on the known path and compare it against the path you're testing?if [ -L "$path" ]
) unless you have a good reason not to and you're sure you know what you're doing.