使用Homebrew安装Bash 5.1.16(1),我如何编写Shell脚本以使用字符串资本化功能?

发布于 2025-02-02 20:36:26 字数 667 浏览 5 评论 0原文

因此,以下基本脚本应输出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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2025-02-09 20:36:26

在您的脚本中,您使用:

#! /bin/bash

这是Apple提供的系统bash,该系统为Macos Monterey提供了3.2.57(1)-Release。您希望使用的此功能可在Bash 4+中使用。它将忽略您的登录外壳,并在test.sh文件中使用硬编码的弹壳。

您已经从Brew中安装了Bash 5,因此将脚本更改为:

#!/usr/bin/env bash

或更改为:

#!/opt/homebrew/bin/bash

In your script, you use:

#! /bin/bash

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 the test.sh file.

You have installed bash 5 from brew, so change the script to:

#!/usr/bin/env bash

Or change to:

#!/opt/homebrew/bin/bash
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文