这个小写表达式在 bash 中如何工作

这个小写表达式在 bash 中如何工作

我想将变量值转换为小写。为此,我执行以下操作

lower_case_var=echo ${var,,}

上面的表达式运行良好。但是,我想知道它是如何工作的?

答案1

部分参数扩展(摘录)自 bash 的手册页:

   ${parameter,,pattern}
          Case modification.   This  expansion  modifies  the  case  of
          alphabetic  characters in parameter.  The pattern is expanded
          to produce a pattern just as in pathname  expansion.   The  ^
          operator  converts  lowercase  letters  matching  pattern  to
          uppercase; the , operator converts matching uppercase letters
          to  lowercase.  The ^^ and ,, expansions convert each matched
          character in the expanded value; the ^ and , expansions match
          and  convert  only the first character in the expanded value.
          If pattern is omitted, it is treated like a ?, which  matches
          every  character.  If parameter is @ or *, the case modifica‐
          tion operation is applied to  each  positional  parameter  in
          turn,  and the expansion is the resultant list.  If parameter
          is an array variable subscripted with @ or *, the case  modi‐
          fication  operation is applied to each member of the array in
          turn, and the expansion is the resultant list.

相关内容