Bash function - return parent script file path -
i have bash script containing function sourced number of different bash scripts. function may fail based on input, , i'd create logging within function identify script(s) causing failures.
e.g., source /path/to/function.sh
the closest i've come this:
ps --no-heading -ocmd -p $$
this works enough if full file path used run parent script, returning:
/bin/bash /path/to/parent.sh
but fails provide full path if parent script run relative path, returning:
/bin/bash ./parent.sh
ideally, i'd way reliably return parent script file path both cases.
i suppose have each parent script pass file path function (via $0 or similar), seems hard enforce , not terribly elegant.
any ideas, or alternative approaches? should not worry relative path case, , use full/absolute file paths everything?
thanks!
i'm using centos 5.9. bash version - gnu bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
you can use readlink follow symbolic links absolute path.
echo $(readlink -f $0)
Comments
Post a Comment