bash - 大写下划线后的第一个字符

标签 bash

我想大写第一个字符和下划线后的每个第一个字符。例如:

foo="text_search";

//uppercase first characters

echo $foo;

应该输出“Text_Search”

我尝试了以下方法:

UCF_textSearch=${foo^}

echo UCF_textSearch

但它输出“Text_search”而不是“Text_Search”。

最佳答案

使用 GNU sed 的解决方案:

foo="the_quick_brown_dog"
foo=$(echo "$foo" | sed -e 's/\(^\|_\)\([a-z]\)/\1\u\2/g')
echo $foo

结果:

$ ./test.sh
The_Quick_Brown_Dog

关于bash - 大写下划线后的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53459645/

相关文章:

linux - 在模式之后添加字符串不起作用

bash - 在返回/退出函数之前弹出

java - -bash : syntax error near unexpected token '(' when running Java program from command line

bash - 使用 bash 删除子域

bash - 无法测试文件是否存在于 Bash 中

linux - 使用 Shell 通过 ssh 从远程机器收集 Full GC 计数

bash - 如何限制绕过 bash ssh 隧道

python - 无法在 MacOS : 'zsh: command not found: nvm' 上使用 nvm install

linux - 使用 sed 替换字符串的一部分

linux - 为什么我的 git auto-update Expect 脚本不起作用?