1

In the Shell script code, the command line arguments assigned to variable like below. what does it mean comma(,) in the statement. What will be the difference when comma is added twice while reading the command line arguments in Bash script.

#!/bin/bash
var1=${1,,}
var2=${2,,}

./script.sh value1 value2
2
  • 1
    Are you sure it's not ./script "$var1" "$var2" instead? Oct 9, 2017 at 13:05
  • @StéphaneChazelas - Its not "$var1" "$var2". Inside the script, while assigning the value there is double comma.
    – intechops6
    Oct 10, 2017 at 7:32

2 Answers 2

4

It's a parameter expansion called Case modification (see man bash).

$var1 will contain the first argument with all characters converted to lowercase. Single , would only change the first character of the parameter.

You can specify a pattern for each character after the comma(s), e.g. the following will only lowercase vowels:

x=$(echo {A..Z})
echo ${x,,[AEIOU]}

Output:

a B C D e F G H i J K L M N o P Q R S T u V W X Y Z

Symmetrically, you can use ^ to convert to upper case.

1
  • Nice to know these features. Maybe, only bash man pages provides lot of advanced stuff.
    – intechops6
    Oct 9, 2017 at 12:46
2
man bash | grep -B1 -A10 ,,
       ${parameter,pattern}
       ${parameter,,pattern}
              Case modification.  This expansion modifies the case  of  alpha‐
              betic  characters in parameter.  The pattern is expanded to pro‐
              duce a pattern just as in pathname expansion.  Each character in
              the  expanded value of parameter is tested against pattern, and,
              if it matches the pattern, its case is converted.   The  pattern
              should  not  attempt  to  match  more than one character.  The ^
              operator converts lowercase letters matching pattern  to  upper‐
              case; the , operator converts matching uppercase letters to low‐
              ercase.  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  modification  operation  is
              applied  to each positional parameter in turn, and the expansion
              is the resultant list.  If parameter is an array  variable  sub‐
              scripted with @ or *, the case modification operation is applied
              to each member of the array in turn, and the  expansion  is  the
              resultant list.
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .