Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash: cannot find track subdirectory #10

Closed
glennj opened this issue Feb 16, 2021 · 6 comments
Closed

bash: cannot find track subdirectory #10

glennj opened this issue Feb 16, 2021 · 6 comments
Labels
bug Something isn't working plugin Related to the plugin's main script

Comments

@glennj
Copy link
Contributor

glennj commented Feb 16, 2021

In bash, when sourcing a file, $0 is not the path of the file being sourced: that is the first element of the BASH_SOURCE array.

If I add a debugging line into xr.sh:

_dir="$(dirname "$(readlink -f "$0")")"
printf ">>%s\n" "$0" "$_dir" "${BASH_SOURCE[0]}"

Then:

$ cd
$ . ~/src/exercism/PaulDance/xr/xr.sh
>>bash
>>/Users/glennjackman
>>/Users/glennjackman/src/exercism/PaulDance/xr/xr.sh
@glennj
Copy link
Contributor Author

glennj commented Feb 16, 2021

Perhaps something gross like

if [[ -n $ZSH_VERSION ]]; then
    _dir="$(dirname "$(readlink -f "$0")")"
elif [[ -n $BASH_VERSION ]]; then
    _dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
fi

or

if [[ -n $BASH_VERSION ]]; then
    _script=${BASH_SOURCE[0]}
else
    _script=$0
fi
_dir="$(dirname "$(readlink -f "${_script}")")"
unset _script

@PaulDance
Copy link
Owner

Interesting incompatibility. Is Zsh the only one treating $0 as the sourced file or is Bash the only one using BASH_SOURCE like your second solution suggests?

@glennj
Copy link
Contributor Author

glennj commented Feb 16, 2021

It seems this is something special that both bash and zsh do.

@PaulDance
Copy link
Owner

Alright, thanks for the research. Let's adopt the second solution for now, as it fixes the current problem. If someone uses Ksh, then let's wait for them to complain if $0 does not work. I'll try to get more information on my side in the meantime.

@glennj
Copy link
Contributor Author

glennj commented Feb 16, 2021

Oops, I didn't see your comment before creating the PR.

@PaulDance
Copy link
Owner

PaulDance commented Feb 16, 2021

No problem! I was actually peeling and cutting up some vegetables, so I couldn't really do some more research... Fixing this is already great. Further improvements can always wait.

@PaulDance PaulDance added bug Something isn't working plugin Related to the plugin's main script labels Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working plugin Related to the plugin's main script
Projects
None yet
Development

No branches or pull requests

2 participants