In Fish, how do I check that two paths are the same?

33 Views Asked by At

I'm trying to write an if statement that checks to see if two variables refer to the same file or directory. I can't simply compare the strings, because I want two different strings that refer to the same path.

For example, I would like to check if ./ and ../foobar/ refer to the same directory.

Simply doing test "$path1" -eq "$path2" won't work in some cases.

1

There are 1 best solutions below

0
Flimm On

The answer is found in the documentation for the test command:

Operators to compare files and directories

FILE1 -ef FILE1

Returns true if FILE1 and FILE2 refer to the same file.

Here's an example:

 if test "$path1" -ef "$path2"
      echo "These two variables refer to the same file"
 end

Note that the two paths have to refer to a file or directory that exists. If foobar does not exist, then test foobar -ef foobar will evaluate to false.