What\'s the best way to check if two paths are equal in Bash? For example, given the directory structure
~/
Desktop/
Downloads/ (symlink to ~/Downloads
Native bash way:
pwd -P
returns the physical directory irrespective of symlinks.
cd "$dir1"
real1=$(pwd -P)
cd "$dir2"
real2=$(pwd -P)
# compare real1 with real2
Another way is to use cd -P
, which will follow symlinks but leave you in the physical directory:
cd -P "$dir1"
real1=$(pwd)
cd -P "$dir2"
real2=$(pwd)
# compare real1 with real2