Issue
I'm trying to get the full path of a file excluding the file name. I'm aware of this approach:
$ var1="$(readlink -f test.sh)"
$ var2="$(basename test.sh)"
$ echo "$var1 $var2"
/home/peki/CodeRepos/shellScripts/test.sh test.sh
$ echo "${var1%"$var2"}"
/home/peki/CodeRepos/shellScripts/
But how would I do it by just manipulating string literals produced by function calls; i.e., how would I do it without storing "$(readlink -f test.sh)"
and "$(basename test.sh)"
in variables?
Solution
I believe this should work:
echo "$(cd "$(dirname "test.sh")"; pwd -P)"
All this is doing is (in a subshell) cd'ing to the directory containing "test.sh" and using pwd to print that directory.
Answered By - CheChe