我想在bash中大写一个变量;我安装了版本4.4.12(1),因此使用${variable^^}
语法。
简单的测试脚本是:
#!/usr/bin/env bash
echo $(bash --version)
words="test string"
uppercase_words=${words^^}
echo "words '${words}' uppercased '${uppercase_words}'"
从bash(bash case_test
或./case_test
)运行时,它可以正常运行:
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0) Copyright (C) 2016 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.
words 'test string' uppercased 'TEST STRING'
在源文件(source case_test
)时,它会抛出“错误替换”:
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0) Copyright (C) 2016 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.
-bash: ${words^^}: bad substitution
words 'test string' uppercased ''
这到底是怎么回事?它就像它正在获得旧版本的bash,但正如你所看到的,版本对于任一场景都显示相同的内容。我在macOS Sierra上,用brew来获取最新版本的bash。
更新和修复: 关于$ BASH_VERSION和源操作的评论(感谢@Gordon&amp; @Charles)是关键。我一直依赖sh-bang声明处理事情,但是源不使用sh-bang。
我通过chsh
命令将shell更改为brew版本来解决问题。