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.
3 Answers
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.
Comments
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
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
/bin/sh instead of /bin/bash in the shebang?
#!/bin/shshebang should catch many bash-isms. How large are these scripts? How portable, exactly, do you need them to be?bashis one. Are you talking about compatibility with the Bourne Shell (sh), specifically?