4

I think I have configured bash as shell, but why is this happening?

My shell is bash:

# ls -al `which sh`
  lrwxrwxrwx 1 root root 4 Jul 12 03:25 /bin/sh -> bash 

Error executing a script with sh

# sh ubuntu/util.sh
  ubuntu/util.sh: line 32: `test-build-release': not a valid identifier

No error from bash

# bash ubuntu/util.sh
13

It is running the bash executable you expect, but your shell is not simply bash; it is bash running in POSIX sh compatibility mode. It does this by looking at the command name it was invoked as, and if that is sh, then it automatically activates POSIX mode. It does a very terrible job at this, and should not be fully relied on for testing POSIX sh compatible scripts, but it should considerably restrict allowed syntax.

--posix

Change the behavior of bash where the default operation differs from the POSIX standard to match the standard (posix mode). See SEE ALSO below for a reference to a document that details how posix mode affects bash's behavior.

And from http://tiswww.case.edu/php/chet/bash/POSIX :

Starting Bash with the --posix command-line option or executing set -o posix while Bash is running will cause Bash to conform more closely to the POSIX standard by changing the behavior to match that specified by POSIX in areas where the Bash default differs.

When invoked as sh, Bash enters POSIX mode after reading the startup files.

  • Thank you, that should be the explanation. I am not running this script directly, I am trying to install Kubernetes and after a lot of work it fails with 'not a valid identifier' error. Can I switch Posix off globally? – Assen Kolov Jul 12 '16 at 8:01
  • not sure on a clean solution... but for a dirty hack, remove sh, replace it with a shell script that runs bash "$@"; exit $? – Peter Jul 12 '16 at 8:03
  • 1
    and I must add that I find this funny...because it's probably just like I said "should not be fully relied on for testing POSIX"... the author of your script probably only tested with bash, not sh, and not the same version of it as you are using. :) – Peter Jul 12 '16 at 8:04
  • 1
    @AssenKolov issue: while "-" used in function is fine for bash, it is not for sh, see line number 32 – Rahul Jul 12 '16 at 8:04
  • This sounds genius to me :-) I can't right now but will let you know after I do. – Assen Kolov Jul 12 '16 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.