How to check if two paths are equal in Bash?

前端 未结 5 622
渐次进展
渐次进展 2021-01-03 20:54

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         


        
5条回答
  •  礼貌的吻别
    2021-01-03 21:29

    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
    

提交回复
热议问题