变量名后的2个逗号在bash中是什么意思?

use*_*345 22 linux bash shell

最近我遇到了这种语法:

${reportName,,}
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索找不到任何东西,所以有谁知道这些是什么意思?

P..*_*... 30

这称为bash版本4+中的"参数扩展".要将存储在变量中的字符串的大小写更改为小写.Eg:

var=HeyThere
echo ${var,,}
heythere
Run Code Online (Sandbox Code Playgroud)

您可能想尝试一些其他命令并检查效果:source

${var^}
${var^^}
${var,}
${var,,}
Run Code Online (Sandbox Code Playgroud)

注意:"参数扩展"存在于man bash.Search中.

  • @chepner 已删除,为了安全起见更好。 (3认同)
  • 小心; `~` 和 `~~` 目前没有记录,所以我会避免使用它们。 (2认同)
  • 在 [Bash 参考手册 →3.5.3 Shell 参数扩展](https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion) 中找到它们。 (2认同)

scr*_*row 9

正如 P 所提到的...这是参数扩展。下面的例子。

myword="Hello"
echo ${myword^}  # first letter uppercase
echo ${myword^^} # all uppercase
echo ${myword,}  # first letter lowercase
echo ${myword,,} # all lowercase
echo ${myword~}  # reverses the case for the first letter
echo ${myword~~} # reverses the case for every letter
Run Code Online (Sandbox Code Playgroud)

输出:

Hello
HELLO
hello
hello
hello
hELLO
Run Code Online (Sandbox Code Playgroud)