$BASHPID 的可移植性

$BASHPID 的可移植性

我发现了一些不一致的行为,$BASHPID如下所示:

# On Mac Yosemite
echo $BASH_VERSION ${BASH_VERSINFO[5]} $BASHPID
# => 3.2.57(1)-release x86_64-apple-darwin14

# On Ubuntu
echo $BASH_VERSION ${BASH_VERSINFO[5]} $BASHPID
# => 4.3.11(1)-release x86_64-pc-linux-gnu 29134

Mac OSX不$BASHPID存在,用它来编写可移植脚本不安全吗?

答案1

内置变量$BASHPID是在 bash 4.0 版本中引入的。

NEWS查看bash源代码中的文件:

 397 -------------------------------------------------------------------------------
 398 This is a terse description of the new features added to bash-4.0 since
 399 the release of bash-3.2.  As always, the manual page (doc/bash.1) is
 400 the place to look for complete descriptions.
 401 
 402 1.  New Features in Bash
 403 
 ...
 410 c.  There is a new variable, $BASHPID, which always returns the process id of
 411     the current shell.

相关内容