Article View: linux.debian.user
Article #310Re: Bash: brace expansion [was: Bash: check if a value is into an array]
From: Greg Wooledge
Date: Mon, 26 Oct 2026 22:30
Date: Mon, 26 Oct 2026 22:30
68 lines
4236 bytes
4236 bytes
On Mon, Oct 26, 2026 at 20:59:39 +0100, john doe wrote:
> On 10/26/26 8:20 PM, Franco Martelli wrote:
> > On 26/10/26 at 19:38, Greg Wooledge wrote:
> > > On Mon, Oct 26, 2026 at 19:21:48 +0100, Franco Martelli wrote:
> > > > As usual thanks Greg for the clarifications. I'm still playing
> > > > with
> > not output, as input to populate the array, John Doe suggested:
> >
> > playlist=($(printf "%d\n" {001..136}))
> >
> "playlist=($(printf "%s\n" {001..136}))" looks to be a tad better!
They're both insane. Neither one makes any sense!
An array *does not contain a newline-delimited string of data*. It
contains string elements, and each string element is pointed to by
a non-negative integer index.
Consider these arrays:
a=( 1 2 3 4 )
b=( $(echo "1 2 3 4") )
c=( $(printf '%s\n' {1..4}) )
What are the differences among them?
It's a trick question! There are no differences! After running those
lines of code, we have:
hobbit:~$ declare -p a b c
declare -a a=([0]="1" [1]="2" [2]="3" [3]="4")
declare -a b=([0]="1" [1]="2" [2]="3" [3]="4")
declare -a c=([0]="1" [1]="2" [2]="3" [3]="4")
They're identical.
If you're populating an array using Word Splitting applies to a Command
Substitution (which is what b and c are using), it doesn't *matter*
whether the string elements are separated by spaces, tabs, or newlines.
Word Splitting treats them all the same.
Look at the assignment of c in detail. We have an inner command (inside
the Command Substitution) of
printf '%s\n' {1..4}
This generates the following string of characters:
1
2
3
4
When Word Splitting is done on this string, the results are the words
1, 2, 3, and 4. So, those become the array's elements. It's exactly
the same as if you had done:
c=(
1
2
3
4
)
(Yes, that is valid syntax.) You get the same array in the end.
So my question remains: *why* do you want a newline-delimited string
of numbers?
Technical Details
Message-ID:
<NSBmx-85yG-13@gated-at.bofh.it>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomodReferences:
<NR1br-7bQc-11@gated-at.bofh.it> <NRohI-7plk-23@gated-at.bofh.it> <NRohI-7plk-21@gated-at.bofh.it> <NRpnr-7pXh-17@gated-at.bofh.it> <NSvqO-82wf-1@gated-at.bofh.it> <NSwwx-834K-1@gated-at.bofh.it> <NSxCh-83C3-1@gated-at.bofh.it> <NSxLX-83Fh-7@gated-at.bofh.it> <NSBmx-85yG-15@gated-at.bofh.it> <NSBmx-85yG-17@gated-at.bofh.it>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod From: Greg Wooledge <greg@wooledge.org> Newsgroups: linux.debian.user Subject: Re: Bash: brace expansion [was: Bash: check if a value is into an array] Date: Mon, 26 Oct 2026 23:30:01 +0100 Message-ID: <NSBmx-85yG-13@gated-at.bofh.it> References: <NR1br-7bQc-11@gated-at.bofh.it> <NRohI-7plk-23@gated-at.bofh.it> <NRohI-7plk-21@gated-at.bofh.it> <NRpnr-7pXh-17@gated-at.bofh.it> <NSvqO-82wf-1@gated-at.bofh.it> <NSwwx-834K-1@gated-at.bofh.it> <NSxCh-83C3-1@gated-at.bofh.it> <NSxLX-83Fh-7@gated-at.bofh.it> <NSBmx-85yG-15@gated-at.bofh.it> <NSBmx-85yG-17@gated-at.bofh.it> X-Mailbox-Line: From debian-user-request@lists.debian.org Mon Oct 26 22:19:40 2026 Old-Return-Path: <greg@wooledge.org> X-Amavis-Spam-Status: No, score=-1.9 tagged_above=-10000 required=5.3 tests=[BAYES_00=-1.9] autolearn=ham autolearn_force=no X-Policyd-Weight: using cached result; rate: -4.6 Mail-Followup-To: debian-user@lists.debian.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Mailing-List: <debian-user@lists.debian.org> archive/latest/832630 List-ID: <debian-user.lists.debian.org> List-URL: <https://lists.debian.org/debian-user/> List-Archive: https://lists.debian.org/msgid-search/20261026221426.GC6334@wooledge.org Approved: robomod@news.nic.it Lines: 68 Organization: linux.* mail to news gateway Sender: robomod@news.nic.it X-Original-Date: Mon, 26 Oct 2026 18:14:27 -0400 X-Original-Message-ID: <20261026221426.GC6334@wooledge.org> X-Original-References: <02f8c2b4-80ec-4f0a-a277-3f1c80acd476@gmail.com> <6ADA15E3.2000204@fastmail.fm> <sm0se1wfqki.fsf@lakka.kapsi.fi> <att7I4GKv9T6vB22@tuxteam.de> <e5212026-5fa3-4d52-8ab6-19befb78fc56@gmail.com> <20261026171539.GA6334@wooledge.org> <635e7678-596e-4f9a-9d2d-cf2c22b22a70@gmail.com> <20261026183801.GB6334@wooledge.org> <3a818795-6036-4773-ae3b-69b63419bec2@gmail.com> <529bc880-00ac-4620-af81-ebc9db5bcbff@mail.com>