使用Homebrew安装Bash 5.1.16(1),我如何编写Shell脚本以使用字符串资本化功能?
因此,以下基本脚本应输出test
,但是我在基于M1的MacOS上看到$ {var ^^}:不良替换
。
#!/bin/bash
var='test'
echo ${var^^}
我的登录外壳来自Homebrew:
$ echo ${BASH_VERSION}
5.1.16(1)-release
$ which bash
/opt/homebrew/bin/bash
它安装在 /etc /shells中:
$ grep homebrew /etc/shells
/opt/homebrew/bin/bash
每个bash的版本:
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
Laptop:bash$ /opt/homebrew/bin/bash --version
GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
So the below basic script should output TEST
, but instead I see ${var^^}: bad substitution
on my M1 based macOS.
#!/bin/bash
var='test'
echo ${var^^}
My login shell is from homebrew:
$ echo ${BASH_VERSION}
5.1.16(1)-release
$ which bash
/opt/homebrew/bin/bash
It is installed in /etc/shells:
$ grep homebrew /etc/shells
/opt/homebrew/bin/bash
The versions of each bash:
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
Laptop:bash$ /opt/homebrew/bin/bash --version
GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的脚本中,您使用:
这是Apple提供的系统bash,该系统为Macos Monterey提供了
3.2.57(1)-Release
。您希望使用的此功能可在Bash 4+中使用。它将忽略您的登录外壳,并在test.sh
文件中使用硬编码的弹壳。您已经从Brew中安装了Bash 5,因此将脚本更改为:
或更改为:
In your script, you use:
This calls the system bash supplied by Apple, which for macOS Monterey is
3.2.57(1)-release
. This feature you wish to use is available in Bash 4+. It will ignore your login shell and use the hardcoded one in thetest.sh
file.You have installed bash 5 from brew, so change the script to:
Or change to: