I wrote a function to resolve the full path to the current Bash script, and I'm wondering if it's SOLID:
#!/usr/bin/env bash
scriptpath()
{
result=${BASH_SOURCE[0]}
# unfortunately readlink -f isn't available on Macs,
# so resolve the symlinks manually
while [ -h "$result" ]
do
symdir=$(dirname "$result")
cd -P "$symdir"
result=$(readlink "$result")
done
echo "$result"
}
echo "The path to the current script is: $(scriptpath)"
Is there anything I may have looked over in the function? I'm not sure because my answer seems quite different than the solutions listed here or here, and I'm pretty fuzzy on symlinks.
I basically want to know if my function (scriptpath
) is any different than this on a Linux system:
readlink -f "${BASH_SOURCE[0]}" | xargs -0 dirname
-L
instead of-h
in yourwhile
test as-h
has been deprecated (they want to drop it but it would break scripts so it remains for now. Probably since-h
is so commonly used for help.) \$\endgroup\$ – DocSalvager May 3 '16 at 9:55