Find bash script location

Standard

This can be useful if you need to cd to the script location from within crontab. cd to the script location from within the script itself !

For the relative path (i.e. the direct equivalent of Windows’ 

):


echo “$MY_PATH”[/crayon]
For the absolute, normalized path:

” # relative
MY_PATH=”

” # absolutized and normalized
if [ -z “$MY_PATH” ] ; then
# error; for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1 # fail
fi
echo “$MY_PATH”[/crayon]

700 total views, 1 views today