Skip to main content
added 294 characters in body
Source Link
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

Using [*] will create a single string, with each element of your array delimited byjoined with the first character of $IFS (a space by default) in between elements¹.

Using [@] will create a list.

Examples:

Creating an array:

$ list=( a b "big fish" c d ) 

Printing each element individually:

$ printf 'data: ---%s---\n' "${list[@]}" data: ---a--- data: ---b--- data: ---big fish--- data: ---c--- data: ---d--- 

Creating a single string and printing that:

$ printf 'data: ---%s---\n' "${list[*]}" data: ---a b big fish c d--- 

Again, but with a custom delimiter:

$ IFS='/' $ printf 'data: ---%s---\n' "${list[*]}" data: ---a/b/big fish/c/d--- 

Note that using these expansions without double quotes rarely makes sense.


¹ or nothing if $IFS is set but empty (after IFS=) or a space if $IFS is unset (after unset -v IFS) with some variations between shells if the first character of $IFS is a multibyte character, or something that cannot be decoded as a character.

Using [*] will create a single string, with each element of your array delimited by the first character of $IFS (a space by default).

Using [@] will create a list.

Examples:

Creating an array:

$ list=( a b "big fish" c d ) 

Printing each element individually:

$ printf 'data: ---%s---\n' "${list[@]}" data: ---a--- data: ---b--- data: ---big fish--- data: ---c--- data: ---d--- 

Creating a single string and printing that:

$ printf 'data: ---%s---\n' "${list[*]}" data: ---a b big fish c d--- 

Again, but with a custom delimiter:

$ IFS='/' $ printf 'data: ---%s---\n' "${list[*]}" data: ---a/b/big fish/c/d--- 

Note that using these expansions without double quotes rarely makes sense.

Using [*] will create a single string, with each element of your array joined with the first character of $IFS (a space by default) in between elements¹.

Using [@] will create a list.

Examples:

Creating an array:

$ list=( a b "big fish" c d ) 

Printing each element individually:

$ printf 'data: ---%s---\n' "${list[@]}" data: ---a--- data: ---b--- data: ---big fish--- data: ---c--- data: ---d--- 

Creating a single string and printing that:

$ printf 'data: ---%s---\n' "${list[*]}" data: ---a b big fish c d--- 

Again, but with a custom delimiter:

$ IFS='/' $ printf 'data: ---%s---\n' "${list[*]}" data: ---a/b/big fish/c/d--- 

Note that using these expansions without double quotes rarely makes sense.


¹ or nothing if $IFS is set but empty (after IFS=) or a space if $IFS is unset (after unset -v IFS) with some variations between shells if the first character of $IFS is a multibyte character, or something that cannot be decoded as a character.

Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

Using [*] will create a single string, with each element of your array delimited by the first character of $IFS (a space by default).

Using [@] will create a list.

Examples:

Creating an array:

$ list=( a b "big fish" c d ) 

Printing each element individually:

$ printf 'data: ---%s---\n' "${list[@]}" data: ---a--- data: ---b--- data: ---big fish--- data: ---c--- data: ---d--- 

Creating a single string and printing that:

$ printf 'data: ---%s---\n' "${list[*]}" data: ---a b big fish c d--- 

Again, but with a custom delimiter:

$ IFS='/' $ printf 'data: ---%s---\n' "${list[*]}" data: ---a/b/big fish/c/d--- 

Note that using these expansions without double quotes rarely makes sense.