Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parent folder of executing script in zsh?

Tags:

bash

zsh

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?

like image 340
don_jones Avatar asked Jul 27 '12 09:07

don_jones


2 Answers

I now realized that $0 in zsh behaves like $BASH_SOURCE in bash. So using $BASH_SOURCE when available and falling back to $0 solves my problem:

${BASH_SOURCE:-$0}

There is a little zsh edge case left, when sourcing from $PATH like:

zsh> cat ../script
echo \$0: $0
echo \$BASH_SOURCE: $BASH_SOURCE
echo '${BASH_SOURCE:-$0}:' ${BASH_SOURCE:-$0}
zsh> . script
$0: script
$BASH_SOURCE:
${BASH_SOURCE:-$0}: script
bash> . script
$0: bash
$BASH_SOURCE: /home/me/script
${BASH_SOURCE:-$0}: /home/me/script

I could do a which script but this would not play nice with other cases

like image 199
don_jones Avatar answered Oct 29 '22 22:10

don_jones


While it would be easy to do this in zsh, it is just as easy to use pure bash which is able to be evaluated in zsh. If you cannot use any command that may or may not be on your path, then you can only use variable alteration to achieve what you want:

SCRIPT_SOURCE=${0%/*}

This is likely to be a relative path. If you really want the full path then you will have to resort to an external command (you could implement it yourself, but it would be a lot of work to avoid using a very available command):

SCRIPT_SOURCE=$(/bin/readlink -f ${0%/*})

This doesn't depend on your $PATH, it just depends on /bin/readlink being present. Which it almost certainly is.

Now, you wanted this to be a sourced file. This is fine, as you can just export any variable you set, however if you execute the above then $0 will be the location of the sourced file and not the location of the calling script.

This just means you need to set a variable to hold the $0 value which the sourced script knows about. For example:

The script you will source:

echo ${LOCATION%/*}

The script that sources that script:

LOCATION=$0
<source script here>

But given that the ${0%/*} expansion is so compact, you could just use that in place of the script.

like image 42
Matthew Franglen Avatar answered Oct 29 '22 22:10

Matthew Franglen