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

Expansion of $0 changed in 3.5.0 release even for bash scripts #9143

Closed
vlitvin opened this issue Aug 15, 2022 · 2 comments
Closed

Expansion of $0 changed in 3.5.0 release even for bash scripts #9143

vlitvin opened this issue Aug 15, 2022 · 2 comments
Labels
bug Something that's not working as intended
Milestone

Comments

@vlitvin
Copy link

vlitvin commented Aug 15, 2022

Expansion of $0 differs from one in bash and zsh in script, even when shell explicitly set with shebang. My test show that this behaviour changed in 3.5.0 resulting in broken bash scripts.

Fish version:

  fish --version
  fish, version 3.5.1

System version:

$ uname -a
Darwin XXXXX-MacBook-Pro.local 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64 x86_64
$ echo $TERM
xterm-256color

I've tried sh -c 'env HOME=$(mktemp -d) fish' and got same results

Steps to reproduce:

# Create simple bash script
echo <<EOF > ./expansion.sh
#!/usr/bin/env bash

echo $0
EOF

# Make it executable
chmod +x ./expansion.sh

# Observe different output
./expansion.sh
<absolute_path>/expansion.sh

bash ./expansion.sh
./expansion.sh

zsh ./expansion.sh
./expansion.sh

I know that compatability with other shells not guaranteed, but it would be nice to have at least clear statement about it in changelog.
Though, it's not crystal clear to me, why behaviour differs even with shebang.

@faho
Copy link
Member

faho commented Aug 15, 2022

Bisects to ed78fd2.

Note:

t this behaviour changed in 3.5.0 resulting in broken bash scripts.

If your bash script is "broken" by this, it is already broken. $0 can always be absolute. Call $PWD/expansion.sh in bash, and it will be absolute. Call it as e.g. /path/to/script/expansion.sh from elsewhere, and it'll be absolute.

$0 is one of those brilliant unix features that appear to be nice and simple until you figure out they aren't. $0 can be any relative or absolute path to your script - unless you tell the shell to use another value:

bash -c '. ./expansion.sh' /bin/grep

Now your script thinks it is /bin/grep in bash and dash, but './expansion.sh' in zsh.


I'd probably lean towards changing this back because it wasn't supposed to be changed, but your use of $0 isn't actually any good, and it's not like we pass any arbitrary value here. It's a perfectly cromulent path to the script.

@faho faho added the bug Something that's not working as intended label Aug 15, 2022
@faho faho added this to the fish-future milestone Aug 15, 2022
@faho faho closed this as completed in 40733ca Aug 15, 2022
@faho faho modified the milestones: fish-future, fish 3.6.0 Aug 15, 2022
@vlitvin
Copy link
Author

vlitvin commented Aug 16, 2022

I've got your point. My bash scripts will fail in certain circumstances depending on how I invoke them.

Thanks you for fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that's not working as intended
Projects
None yet
Development

No branches or pull requests

2 participants