LinuxCommandLibrary

comma

brace expansion and arithmetic separator in shell

TLDR

Brace expansion with sequence

$ echo {a,b,c}
copy
Generate multiple filenames
$ touch file{1,2,3}.txt
copy
Multiple values in arithmetic
$ (( a=1, b=2, c=a+b ))
copy
Sequential commands (expr1, expr2)
$ for ((i=0, j=10; i<j; i++, j--)); do echo $i $j; done
copy

SYNOPSIS

{item1,item2,...}

DESCRIPTION

The comma has several uses in shell scripting:
Brace expansion: {a,b,c} expands to a b c. Combined with text: file{1,2,3}.txt becomes file1.txt file2.txt file3.txt. Can be nested: {a,b{1,2}} expands to a b1 b2.
Arithmetic comma operator: In ((...)), comma separates expressions evaluated left to right, returning the rightmost value.
For loop multiple expressions: In C-style for loops, comma separates initialization and increment expressions.
Parameter expansion (bash 4+): ${var,,} lowercases, ${var,} lowercases first char.

CAVEATS

Brace expansion happens before variable expansion. {$a,$b} doesn't expand variables first.
No spaces around comma in brace expansion: {a, b} won't expand.
Brace expansion is a bash/zsh feature, not POSIX sh.
The comma operator in ((...)) is rarely needed; semicolons often suffice.

SEE ALSO

bash(1), zsh(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community