49

I have heard about fish that it's a friendly and out-of-box shell but also it doesn't support POSIX standard. On the other hand I read about POSIX standard (and also I tested it on my Fedora, It's amazing and out-of-box shell now I want to change my default shell to fish).

But the matter that I opened this question for is: I misunderstood about relation between fish and POSIX standard, what do you mean about fish does NOT support POSIX exactly? & How? (Should I change my bash to fish?).

Please explain it simple 'cause I'm a little newbie, thanks.

Cy8099
  • 623
  • 1
  • 5
  • 10
  • 4
    The POSIX shell command language standard is given [here](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html). Fish's language is nothing like the one described therein; thus, it does not comply with what it means to be a "POSIX shell", which implies that one can run scripts written for that language without modification. – Charles Duffy Feb 11 '18 at 15:52
  • 5
    The biggest risks in using a non-compliant shell are that you might get in habits that result in buggy code when writing code that well be executed with a compliant shell (this is particular risk for zsh, as it looks enough like a POSIX shell that habits can translate easily and result in only subtle bugs that aren't immediately caught), and you're liable to annoy any coworkers who need to maintain your scripts. :) – Charles Duffy Feb 11 '18 at 15:57
  • 3
    Read the docs: https://fishshell.com/docs/current/design.html would be most relevant – glenn jackman Feb 11 '18 at 16:08
  • 1
    What does "POSIX compliant" even mean? https://stackoverflow.com/questions/11376975/is-there-a-minimally-posix-2-compliant-shell. It may be a blessing in disguise not even trying to be compliant. – Samuel Aug 10 '19 at 03:44

2 Answers2

43

fish isn't and never tried to be compatible with POSIX sh.

This really just means that it's a separate language (like Java, Python or Ruby) rather than an implementation or extension of sh (like Bash, Dash and Ksh).

Obviously, just like you can't copy-paste Java snippets into a Python program, you can't copy-paste sh code into fish.

In practice, this means that when you search for things like "how do I show the current git branch in my prompt", you need to make sure you find fish answers because the sh ones won't work. Similarly, when books or instructions give commands to run, you may occasionally need to rewrite some of them manually (or open a bash shell and paste them there).

Whether this matters is entirely up to you, so definitely give it a go.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 6
    I am not sure "how do I show the current git branch in my prompt" is a good example. In either shell, `git branch` would suffice. Or am I missing something? – Zheng Qu Aug 11 '20 at 20:24
  • 14
    @ZhengQu People frequently add the current branch name to their shell prompt, e.g. `PS1='$(git branch --show-current) \$ '` in bash. If you want to do it in fish, you have to do it in a different way – that other guy Aug 11 '20 at 20:33
  • 1
    Extending the shell is a very different thing from running commands though. "commands to run" implies running commands would be a lot more different than it actually is. Running a command is basically always the same. Adding logic to construct the command and combine commands is not. Using only git commands is not a problem and works just the same in fish. – Kissaki Feb 17 '22 at 18:22
24

Actually, fish is not compliant with the POSIX sh definition. But neither is csh (and probably zsh). You still can use fish as your interactive shell.

For example echo $$ shows the pid of the shell in POSIX sh. But with fish it does not.

(and that is why I did not switch to fish and keep using zsh as my daily interactive login shell)

You could change your interactive login shell (using chsh) to fish.

But if you write shell scripts, writing them for the POSIX sh specification make these scripts more portable. (You'll use the shebang #!/bin/sh to start them, it is understood by Linux execve(2)). In some cases, you don't care about portability of your shell script and you could make them start with #!/usr/bin/fish to be fish scripts. Then they won't work on systems without fish.

Also, the system(3) C standard library function uses /bin/sh -c.

I enjoyed very much Yann Regis-Gianas' talk on POSIX [s]hell at FOSDEM2018.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 6
    zsh is definitely not compliant with its default runtime configuration (the definition is explicit on word-splitting for unquoted expansion, f/e) -- but it does a better job than bash does when started under the name `sh`. – Charles Duffy Feb 11 '18 at 15:53
  • I use fish mainly but if I have to copy and paste something, I just switch to zsh. – samarmohan Feb 13 '21 at 18:12
  • 1
    You can use [bass](https://github.com/edc/bass), a fish plugin that ease the use of bash like commands directly from fish. – Doc Davluz Mar 23 '21 at 10:39