问题
Bash4 offers some nice features like globstar, associative arrays, the mapfile builtin etc.
Will it greatly decrease the portability of my scripts if I make use of these features? Is it safer to stick to Bash 3?
回答1:
Depends on how portable you want to be.
Apple does not ship Bash 4 on Mac OS X since Bash 4 uses the GPLv3. Of course, users can install it themselves if they want, but most users won't have it.
If you want to be portable to OS X, you probably shouldn't rely on Bash 4 features. However, if you don't mind just being portable to modern GNU/Linux distros, then pretty much all of them include Bash 4, so you might as well rely on them.
Also, do recall that not all OS's even ship bash by default. For instance, OpenBSD ships pdksh by default, and you need to explicitly install Bash. Debian ships bash, but /bin/sh
is Dash, so you need to explicitly use #!/bin/bash
if you want to opt in to Bash specific features.
If you really want portability, you should stick to POSIX/Single-Unix-Specification features. However if all you need is to run on the latest GNU/Linux distros (and even ones a few years old), using /bin/bash
and assuming it's Bash 4 should be fine.
来源:https://stackoverflow.com/questions/30708549/is-it-safe-to-use-bash-4-features-in-news-scripts