5

I have a list of scripts written in Bash, and now i need to convert them to Shell scripts. I know bash is an extended implementation of the Shell specification, and so, i'm looking for some specification of the extension implemented in Bash, namely, what's in Bash that is not part of the Shell specification, so i could go over my scripts and make the appropriate modification. Is there such a document? Alternatively, any other suggestion on how to do the conversion will be greatly appreciated.

5
  • 4
    shellcheck with a #!/bin/sh shebang should catch many bash-isms. How large are these scripts? How portable, exactly, do you need them to be? Commented Oct 28, 2015 at 15:11
  • What do you mean by "shell specification"? There are many different shells of which bash is one. Are you talking about compatibility with the Bourne Shell (sh), specifically? Commented Oct 28, 2015 at 15:12
  • 3
    He's probably referring to the POSIX shell specification. Commented Oct 28, 2015 at 15:15
  • And knowing what systems you need to run the shell scripts on would be useful. Commented Oct 28, 2015 at 15:18
  • yes, i did referred the POSIX shell specification. the scripts are pretty long (around 200 scripts, few tens of lines each). and the systems i need to run these scripts are varied, basically, the scripts should run on any *nix system that comes with sh (which is practically every *nix system). Commented Oct 29, 2015 at 10:25

3 Answers 3

8

The shellcheck tool (with a shebang of #!/bin/sh) will catch many bash-isms.

Any that aren't caught should probably be filed as bugs against the project. If they aren't prohibitive to implement they should get added in reasonable time. The author is fairly responsive.

Sign up to request clarification or add additional context in comments.

Comments

4

To change Bash scripts to POSIX compatible shell code you have to remove any bashisms. While I don't think there is any tool out there that can reliably handle all bashisms you can start with checkbashisms.

Comments

2

If you use a Debian based distribution, you can use the checkbashisms command which is provided by the devscripts package.

Here is an example of a bash specific construct (a bashism) highlighed by checkbashisms: using == instead of = in a test.

$ cat b.sh
#!/bin/sh

VAR=foo
if [ $VAR == bar ]; then
    echo 'bar!'
fi
$ checkbashisms b.sh 
possible bashism in b.sh line 4 (should be 'b = a'):
if [ $VAR == bar ]; then

2 Comments

I just tried checkbashisms with the example script given above and didn't get any results. any idea what am i doing wrong? here's the command i'm runing: $checkbashisms test.sh and then the cli returns no output, where test.sh contains the above given script.
@zuckermanori, did you use /bin/sh instead of /bin/bash in the shebang?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.