[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug] Array declaration only includes first element
From: |
Greg Wooledge |
Subject: |
Re: [Bug] Array declaration only includes first element |
Date: |
Thu, 18 Jul 2024 08:57:42 -0400 |
On Thu, Jul 18, 2024 at 00:00:17 +0000, Charles Dong via Bug reports for the
GNU Bourne Again SHell wrote:
> - Declare an array: `a=(aa bb cc dd)`
> - Print this array: `echo $a` or `printf $a`
$a is equivalent to ${a[0]}. That's not how you print an entire array.
The easiest way to print an array is to use "declare -p":
hobbit:~$ a=(aa bb cc dd)
hobbit:~$ declare -p a
declare -a a=([0]="aa" [1]="bb" [2]="cc" [3]="dd")
If you want something a little less noisy, you can use "${a[*]}" to
serialize the whole array to a single string/word, or "${a[@]}" with
the double quotes to expand it to a list of words.
hobbit:~$ echo "<<${a[*]}>>"
<<aa bb cc dd>>
hobbit:~$ printf '<<%s>> ' "${a[@]}"; echo
<<aa>> <<bb>> <<cc>> <<dd>>
See also <https://mywiki.wooledge.org/BashFAQ/005>.