Bash – Is it possible to notify/warn that current path isn’t a valid symlink anymore

bashsymlink

Let's say we have ./a and ./b directories. Then we create a symlink using ln -s a x and entering it cd x The pwd would be x

Then in another terminal we change the x so that it pointed to b. But the first one would still return x for pwd which is not consistent anymore.

So is there a way for current terminal (bash) to alert/warn if the current path is the obsolete symlink?

Best Answer

Here's an approach which uses the fact that bash does command expansion on $PS1 to test to see if . and $PWD have the same inode in the prompt:

PS1='$([[ $(stat -Lc%i $PWD 2>/dev/null) == $(stat -c%i .) ]] || echo "\[\e[1;31m\]PWD invalid or changed \[\e[m\]")'"$PS1"
Related Question