How to run something from the folder the script is in
Lets say I have a script 开发者_Go百科called foo
in some folder and a script called bar
in the same folder. Say foo
looks like this:
./bar
Now if I call ./foo
, this will work fine, but if I am in another folder and call somefolder/foo
, it will fail.
How can I get this to the point where ./bar
is interpreted from the directory that foo
is in?
foo
could look like this:
cd "$(dirname "$0")"
./bar
Answered pretty exhaustively for bash and other shells in:
Getting the source directory of a Bash script from within
精彩评论