Bash 变量转为驼峰式/帕斯卡式

tat*_*tsu 1 bash shell scripting

在 bash 中用大写字母替换某些内容非常简单:

echo to_camel_case__variable | sed -r 's/(.)_+(.)/\1\U\2/g;s/^[a-z]/\U&/'
Run Code Online (Sandbox Code Playgroud)

但如果我不想更换怎么办?如果我只是想让变量帕斯卡大小写,但保留下划线怎么办?

tat*_*tsu 5

这有效:

echo 'this_is_a_test' | sed 's/[^_]\+/\L\u&/g'
Run Code Online (Sandbox Code Playgroud)

我会测试一下,但看起来很可靠。