A simple way to find out the location of a running shell script
When a shell script is executed, the default directory would normally be the current directory of the user when the script is called. If you get into a situation wherein you want to determine the location of the shell script when it was called by the user, you can do the following within the shell script:
fullpath=`which $0`
scriptdir=`dirname ${fullpath}`
The scriptdir variable would contain the directory where the running script is stored. The which program returns the full path of the called script (e.g. /usr/bin/mybashscript.sh) while the dirname program returns the directory portion of the argument which was passed to dirname.
One use for it would be if for example you need to know where the shell script was stored because you need to call another script that was also stored in that directory when it was installed.








Shell





















![QRcode[tag-shell] QRcode:tag-shell](http://www.codestuff.com/wp-content/blogs.dir/7/qrcode-cache/tag-shell.jpg)