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

How to check if the is fish? #374

Closed
maandree opened this issue Oct 29, 2012 · 15 comments
Closed

How to check if the is fish? #374

maandree opened this issue Oct 29, 2012 · 15 comments

Comments

@maandree
Copy link

Is it possbile for a shell script to determine if the executing shell is the fish shell?

@terlar
Copy link
Contributor

terlar commented Oct 29, 2012

Maybe this could work:

➤ basename $SHELL
fish

@maandree
Copy link
Author

basename?

$SHELL is not set by fish.
I do known when $SHELL is set, but on by user it is always /bin/bash which is my default shell.

@terlar
Copy link
Contributor

terlar commented Oct 29, 2012

Ah, I see. Nevermind then, will think about it.

@zanchey
Copy link
Member

zanchey commented Oct 30, 2012

You could check for the existence of variables like __fish_bin_dir, but realistically the shell you are executing is fish if you can parse fish syntax. What are you trying to achieve?

@maandree
Copy link
Author

In case a script is not runnable in fish, I want to suggest other shells to execute with that does work, indeed of have a lot of error messages printed out, alternatively use fish version of the script.

In case the user use fish as his shell, but /bin/sh links to /bin/bash, bash will be the executing shell but fish's variables will still exist, so checking for variables is not safe.

The only safe possibility I can think of is if there is a built in function that will print fish.

@stestagg
Copy link
Contributor

Running a correctly written bash script from fish shouldn't break things. Could you provide a more concrete example?

It may help with understanding what type of detection you need, too.

@maandree
Copy link
Author

shells have different syntax compatibility, for example, bash extends posix;
so it is usable for all shells to implement.

The instruction
(hash chmod 2>/dev/null) || (br=1 ; ro=1 ; echo 'Missing chmod, install coreutils [build+runtime required]'
gives 3 error messages in fish.

@stestagg
Copy link
Contributor

That makes sense, but I'm not sure I understand why you'd want to run a bash command in a fish environment?

if this is in a script, then wouldn't adding #!/bin/bash at the top work?

@maandree
Copy link
Author

It works on all shells that I have have tested except fish, is I think using #!/bin/sh is preferable.

@ridiculousfish
Copy link
Member

The answer at http://stackoverflow.com/questions/3199893/howto-detect-bash-from-shell-script suggests that $BASH_VERSION is an unexported variable, and that zsh follows suit with $ZSH_VERSION.

fish and tcsh both have the version variable; neither of them is exported.

It seems like adding an unexported $FISH_VERSION would be helpful.

@zanchey
Copy link
Member

zanchey commented Oct 31, 2012

Seems like a good idea.

(Note that setting /bin/sh to a non-POSIX-sh-compatible shell is a terrible idea, though, and large parts of your system will probably stop working.)

@maandree
Copy link
Author

Naturally that [setting /bin/sh] is a bad idea.
Roughly how compatible is fish with POSIX?

@ridiculousfish
Copy link
Member

fish's built-ins are generally compliant (e.g. test) but fish itself is not POSIX compliant because it has its own syntax for things like variables. A shell script that does anything more than just run commands will not run successfully in fish.

@zanchey
Copy link
Member

zanchey commented Nov 2, 2012

zanchey@259c6fc

@maandree
Copy link
Author

maandree commented Nov 2, 2012

That should do the trick.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants