Starting with this directory structure:
$ tree
.
├── 1
│ └── 2
│ └── foo.jar
└── a
└── b
└── c
└── setAlias
The goal is to come up with the contents of setAlias, so I can source the file, and it will create an alias that runs java -jar /absolute/path/to/foo.jar
Here's what I have so far:
FOO="java -jar $(realpath $(dirname $_)/../../../1/2/foo.jar)"
echo "Setting Alias:"
echo " foo -> $FOO"
alias foo='$FOO'
If I source setAlias
from its own directly, everything works fine. But if I set it from the root directory, I have to run it twice before the absoulute path is resolved:
$ source a/b/c/setAlias
realpath: ./../../../1/2/foo.jar: No such file or directory
Setting Alias:
foo -> java -jar
$ source a/b/c/setAlias
Setting Alias:
foo -> java -jar /home/MatrixManAtYrService/1/2/foo.jar
If I do this from ./a/b/c
the path is resolved on the first try.
What is happening here? Why does realpath take two tries to find the file?