Convert Bash Scripts To Shell

15,554

Solution 1

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.

Solution 2

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.

Solution 3

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
Share:
15,554

Related videos on Youtube

zuckermanori
Author by

zuckermanori

I live for coding and code for living. Software Engineer and Engineering manager with passion to the technical details.

Updated on June 04, 2022

Comments

  • zuckermanori
    zuckermanori almost 2 years

    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.

    • Etan Reisner
      Etan Reisner over 8 years
      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?
    • chepner
      chepner over 8 years
      He's probably referring to the POSIX shell specification.