即使在 MacOSX 中配置后,我也无法使用 bash 5 语法

ham*_*uki 1 macos bash

我对 bash 比较陌生。

我的 mac 默认安装了 bash 3。

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)

我相信我已经为 MacOSX 安装并配置了 bash 5。运行bash -version命令输出:

GNU bash, version 5.0.3(1)-release (x86_64-apple-darwin18.2.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Run Code Online (Sandbox Code Playgroud)

问题是我无法执行任何 Bash 5 功能,例如此代码使用 bash 4 功能无法输出预期行为:

for i in {1..100..2} ; do
    echo $i
done
Run Code Online (Sandbox Code Playgroud)

输出:

{1..100..2}
Run Code Online (Sandbox Code Playgroud)

预期输出:

1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
Run Code Online (Sandbox Code Playgroud)

rua*_*akh 5

bash -version(或)命令bash --version告诉您如果运行 则将获得的 Bash 版本bash

但我猜你不是手动运行bash;相反,您只是打开 shell,它可能仍然是旧版本的 Bash。(很有可能,在不同位置的系统上安装两个 Bash 副本,这并不罕见。)要检查这一点,您可以运行echo "$BASH_VERSION"以查看您实际输入的 Bash 版本。

要解决此问题,您需要将计算机配置为使用较新版本的 Bash 作为 shell。(或者您可以手动运行bash。)