Search code examples
bashshellposixgit-bash

Why can a script with a "#!/bin/sh" shebang use bash-only features?


When I run the following script :

#!/bin/sh

declare path="my.path"

if [[ "$path" =~ \. ]] ; then
    echo "yes"
else
    echo "no"
fi

I got yes printed.

But if I am not mistaken, declare and [[ ]] are not available in POSIX shell ; so how come this script does not throw any error ?

I run the script on git for Windows, could it explain this odd behavior ?


Solution

  • /bin/sh is a link to your actual shell. It does not mean that you are running pure POSIX. The Git for windows homepage makes very clear that you are running bash, as does your git-bash tag.

    Even on Linux, /bin/sh can still be bash:

    ls -la /bin/sh
    lrwxrwxrwx. 1 root root 4 Mar 27 09:33 /bin/sh -> bash