Is it safe to use Bash 4 features in news scripts?

喜你入骨 提交于 2019-12-12 02:57:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!