Reputation: 498
This Bourne shell script fails to detect the existence of a broken symbolic link. It returns false
and doesn't echo
yet /usr/bin/firefox.real
is a file that exists but as a broken symbolic link. Why?
FIREFOX="/usr/bin/firefox.real"
[ -e "$FIREFOX" ] && echo "exists"
Upvotes: 3
Views: 623
Reputation:
The reason is that internally, bash will call fstat(), not lstat() when you test with -e, so it checks the file itself, not the symbolic link.
Upvotes: 1
Reputation: 785146
Use -h
to check for existence of a link (even broken):
[ -h "$FIREFOX" ] && echo "exists"
As per man test
:
-h FILE
FILE exists and is a symbolic link (same as -L)
Upvotes: 2