在新脚本中使用Bash 4功能是否安全? [英] Is it safe to use Bash 4 features in new scripts?
问题描述
Bash 4提供了一些不错的功能,例如globstar,关联数组,内置的mapfile等.
如果我使用这些功能,是否会大大降低脚本的可移植性?坚持Bash 3更安全吗?
取决于您想要的便携性.
由于Bash 4使用GPLv3,因此Apple不在Mac OS X上发布Bash 4.当然,用户可以根据需要自己安装它,但是大多数用户不会安装它.
如果您想移植到OS X,则可能不应该依赖Bash 4功能.但是,如果您不介意仅将它们移植到现代GNU/Linux发行版中,那么它们几乎都包括Bash 4,因此您最好也依赖它们.
此外,请记住,并非所有操作系统默认情况下甚至都提供bash.例如,默认情况下,OpenBSD附带了pdksh,您需要显式安装Bash.Debian发行了bash,但/bin/sh
是Dash,因此,如果要选择使用Bash特定功能,则需要显式使用#!/bin/bash
./p>
如果您真的想要可移植性,则应坚持使用POSIX/Single-Unix-Specification功能.但是,如果您需要的是在最新的GNU/Linux发行版(甚至是已经发行了几年的发行版)上运行,请使用/bin/bash
并假定它是Bash 4应该没问题.
Bash 4 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?
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.
这篇关于在新脚本中使用Bash 4功能是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!