I am calling a script as below
directory path : /user/local/script/print_path.sh
var_path=`pwd`
echo $var_path
The above script is calling as below directory path : /user/local/callPscript/call.sh
`/user/local/script/print_path.sh`
I want the out put as below :
/user/local/script/
But it gives the output :
/user/local/callPscript/
i.e. the pocation of the script is called. How can I make it to the scripts home directory path?
After some weeks of Bash programming, this has emerged as the standard solution:
directory=$(dirname -- $(readlink -fn -- "$0"))
$0
is the relative path to the script, readlink -f
resolves that into an absolute path, and dirname
strips the script filename from the end of the path.
A safer variant based on the completely safe find:
directoryx="$(dirname -- $(readlink -fn -- "$0"; echo x))"
directory="${directoryx%x}"
This should be safe with any filename - $()
structures remove newlines at the end of the string, which is the reason for the x
at the end.
May be this can help you.
var_path=$PWD
echo $var_path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With