In ksh(1) and bash(1) it is possible to get arg0 of the command line using $0.
Sometimes you need to know the folder/directory from which the script was run. $0 can help us here. $0 will contain the command used to invoke the script, for example
However, non of these (bar the first example) can actually tell us the full path to the directory from which the script was executed. What we need to do is turn the relative paths into absolute ones.
To do this, first recognize that the at the beginning of execution of the script, the path to the script is relative to the current directory. Because of this we can cd(1) to the path part of $0 then use $PWD to obtain the full path of that directory. We need to do this in such a way that it does not disturb the current directory.
The following code does just this (place this code just after the
What this does is it takes $0, removes everything after the last / and cd’s to that directory in a sub-shell, then outputs $PWD which is captured and stored in the variable SCRIPT_DIR. A variation of this which will also work in sh(1)
UPDATE: Here is a much simpler way to achieve this:-