How to get parent folder of executing script in zsh?

Viewed 7835

In bash i get the executing script's parent folder name by this line

SCRIPT_PARENT=`readlink -f ${BASH_SOURCE%/*}/..`

Is there any way to achieve this in zsh in a way that works both in zsh and bash?

Assume i have got a file /some/folder/rootfolder/subfolder/script with the contents:

echo `magic-i-am-looking-for`

I want it to behave this way:

$ cd /some/other/folder
$ . /some/folder/rootfolder/subfolder/script
/some/folder/rootfolder
$ . ../../folder/rootfolder/subfolder/script
/some/folder/rootfolder
$ cd /some/folder/rootfolder
$ . subfolder/script
/some/folder/rootfolder
$ cd subfolder
$ . script
/some/folder/rootfolder

This should work in bash and zsh. My first implements this behavior, but does due to $BASH_SOURCE not work in zsh.

So basically its:

Is there a way to emulate $BASH_SOURCE in zsh, that also works in bash?

3 Answers
Related