7

I have a script where it is defined #!/bin/bash, and I want to check if this script is compatible with #!/bin/sh.

Is there a way to do so?

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 1
    You may try: http://www.shellcheck.net/. However, it is not guaranteed to work even after you've checked that site. Basically you need to review every line manually because when an expression is syntactically correct, it does not mean that it will still do want you want. – hek2mgl Jul 24 '15 at 09:56
  • 4
    Reading up on [Bashisms](http://mywiki.wooledge.org/Bashism) would be a good place to start. – John B Jul 24 '15 at 10:18
  • Related: [Difference between sh and bash](http://stackoverflow.com/q/5725296/113632) – dimo414 Feb 15 '17 at 05:07

2 Answers2

6

Being sh-compatible isn't, in itself, a goal. What issue(s) are you running into that requires your script work with sh? Depending on your reasoning different options may or may not be sufficient. If you simply need your script to run on most modern environments then using bash should be perfectly fine, and may actually be better than using sh, since sh maps to different shells on different platforms. On most modern environments it isn't actually its own binary but is just bash in POSIX-compliant mode, or another shell like dash.

If you really just need to make an existing Bash script work with the shebang #!/bin/sh you have several options:

  1. Just run it as sh your_script.sh and see what happens - Bash is a superset of sh syntax, so many simple Bash scripts are valid sh scripts.
  2. Run sh -n your_script.sh as rojomoke suggests, which will report syntax errors without actually executing the script (but may not catch all issues).
  3. Manually audit your script for invalid syntax (John B's Bashisms reference isn't a bad start), this obviously isn't a great solution but in practice it's the only way to be sure. If that seems daunting, well, it is :)

If you want/need to support sh the best option is simply to specify your script's shebang as #!/bin/sh - if it behaves as desired in the environments you need it to then it's (by definition) sh-compatible.

Note that you can write a sh-compatible script that still isn't POSIX-compliant, since many standard utilities like grep have their own POSIX-compliant feature sets you'd need to respect too. Again though, being POSIX-compliant isn't an end in itself, and I'd encourage you to confirm you really need to support POSIX before trying to be compliant with the standard.

I asked a related question you might find helpful too.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • @hek2mgl yes, it's `dash` on Ubuntu, as mentioned. My point in (1.) is that trying to be `sh` compliant is (often) not that important. What else is "plain wrong/useless", exactly? – dimo414 Feb 15 '17 at 08:05
  • The fact that /bin/bash in POSIX mode might be used for /bin/sh does not mean that it will run any script written for bash. – hek2mgl Feb 15 '17 at 08:08
  • @hek2mgl I don't believe I said that anywhere - in fact I said the opposite: "*many simple* Bash scripts are valid `sh` scripts". This implies there exist Bash scripts, particularly non-trivial ones, which are not valid `sh` scripts. – dimo414 Feb 15 '17 at 08:15
  • Before the discussion goes into the wrong direction, what I want to say is that the task "Make a shell script POSIX compaible" is not trivial and there is no general solution. While you'll first make sure that you will not use *bashisms* in the shell syntax itself, you'll further make sure that any programs like `grep`, `sed`, `awk` etc. etc. etc. will being called in a posixly correct way which can be endlessly painful, depending on the script. – hek2mgl Feb 15 '17 at 08:26
  • @hek2mgl feel free to post your own answer. As best I can tell I'm trying to convey the same thing as you (ideally, just don't try to be `sh`-compatible). Note though that OP isn't (directly) asking for POSIX-compliance, simply `sh` compatibility. Granted they're connected, but they are separate goals. – dimo414 Feb 15 '17 at 08:32
  • Well, unfortunately I don't have an answer :) If you can control the environment in which your scripts are running: make sure they run bash (and friends). If not, probably use something else like Python e.g. if applicable. If not, developing the script in a strict POSIX environment might be a better start. – hek2mgl Feb 15 '17 at 08:34
2

Running the script with the -n option will parse and read the commands, and report syntax errors, but will not execute the code.

sh -n <scriptname>
rojomoke
  • 3,765
  • 2
  • 21
  • 30