-1

I've got a program I've written in bash that uses a number of bashisms. I know there's checkbashism and shellcheck and bash's --posix which are great for manual review. But what I really want is something like python's 2to3 and I'm assuming this exists (if not I'll probably write it).

Essentially I want to continue writing my program in modern bash but also have a build script that runs something like this:

$ bash2posix myprogram.sh > myprogram-compat.sh

and it would "fix" what it can and balk at you if there's things it cannot. I haven't been able to find any evidence of this existing, does anyone know of anything? Thanks.

Edit: I'm pretty sure this can be done. There's usually rather expensive equivalents to a lot of bashisms. String replacement for instance (${//}) can be done with a printf builtin using the precision operator (.) to "search" the string for a given suffix. Then you can test the length of a replacement command to see if you "found" it and then re-constitute a new string with the replacement. Is that efficient? no, of course not, but totally possible.

14
  • 3
    I don't think there can be such a translator. Also note that running bash with --posix in no way disables all extensions that bash normally introduces. – Kusalananda May 25 '20 at 16:21
  • 1
    Perhaps shellcheck could be of use? Change the sh-bang line from bash to sh and see what falls out? – Jeff Schaller May 25 '20 at 16:28
  • 1
    POSIX-correctness is also a matter of using the right commands with the right parameters. I recently witnessed an assumed "posixly correct" script that used stat. – xenoid May 25 '20 at 16:35
  • I mean 1003.2 and 1003.2a posix ... not all of it. – kristopolous May 25 '20 at 16:39
  • 1
    Note also that if you're trying to do this for portability you have to take the tools that the script uses into account, and make the "portable" script implement portable workarounds for non-standard tools used in the script, as well as for extensions used with standard tools, such as mv -t. – Kusalananda May 25 '20 at 22:44
1

It cannot be done. At least not 100%.

I have written env_parallel.dash and env_parallel.bash from GNU Parallel. (https://git.savannah.gnu.org/cgit/parallel.git/tree/src/env_parallel.dash and https://git.savannah.gnu.org/cgit/parallel.git/tree/src/env_parallel.bash)

You cannot convert env_parallel.bash to env_parallel.dash because typeset -f myfunc (show the body of a function) has no equivalent in dash.

3
  • this is so far the only exception I can't find a straightforward fix for. The almquist parser still stores the text in the funcnode list so that set +x can show it but since there's a separation between the vars and funcs in the C code, the variable introspection tools can't really be "tricked" to showing functions - it's a different datastructure ... I'm still not willing to claim it isn't possible. I'd need to get the code to traverse the funcnode linked list and sequentially dump the ->text entries. It'd certainly be a hack ... but this is shell scripting, it's all a hack. – kristopolous May 25 '20 at 19:14
  • @kristopolous I will be happy if you can prove me wrong. – Ole Tange May 25 '20 at 19:32
  • dash has several problems in POSIX compatibility. A major one is missing support for multi byte characters. bosh on the other side supports type -F as a replacement for the ksh-sm typeset -f. – schily May 25 '20 at 19:37

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.