Skip to main content

How to check if a particular path is a working symlink in Bash.

#!/usr/bin/env bash

#
# How to check if a particular path is a symlink
# Ref: How to check if a symlink exists (https://stackoverflow.com/a/36180056)

some_path=path/to/potential/symlink

# To check the existence of a symlink and that it is not broken:
[ -L "${some_path}" ] && [ -e "${some_path}" ]

# The complete solution:
if [ -L "${some_path}" ]; then
    if [ -e "${some_path}" ]; then
        echo "Good symlink"
    else
        echo "Broken symlink"
    fi
elif [ -e "${some_path}" ]; then
    echo "Not a symlink, some other type"
else
    echo "Missing"
fi