🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.debian.user
Article #309

Re: Bash: brace expansion [was: Bash: check if a value is into an array]

#309
From: Greg Wooledge
Date: Mon, 26 Oct 2026 18:40
54 lines
3838 bytes
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 the RNG
> code of the script that shuffle a playlist, since it depends on a list of
> filenames in a given directory, I'd like now that the indexed array
> "playlist" be populated by a sequence of numbers instead of filenames, this
> works:
> 
> playlist=({001..136})

I don't understand the need for this array, but hold that thought.

> but the whole sequence is expanded on a single line. I'd like that each
> number be separated by newline \n.

printf '%d\n' "${playlist[@]}"

I still don't know why you want that output, though.

Now, back to the original thought.  If you've got a playlist array
that contains filenames, then that array is *already* numerically
indexed.

playlist=(clip1 clip2 clip3 clip4)
for i in "${!playlist[@]}"; do
    printf 'Index %d is "%s"\n' "$i" "${playlist[i]}"
done

Running this code gives us:

Index 0 is "clip1"
Index 1 is "clip2"
Index 2 is "clip3"
Index 3 is "clip4"

So, from what I can see, you only want to make two changes:

  1) Count starting from 1 instead of 0.
  2) Zero-pad each index to 3 digits.

printf plus the shell's builtin arithmetic allows us to do this easily:

playlist=(clip1 clip2 clip3 clip4)
for i in "${!playlist[@]}"; do
    printf 'Index %03d is "%s"\n' "$((i+1))" "${playlist[i]}"
done

That gives us:

Index 001 is "clip1"
Index 002 is "clip2"
Index 003 is "clip3"
Index 004 is "clip4"

Is that all you needed?
Technical Details
Message-ID: <NSxLX-83Fh-7@gated-at.bofh.it>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.corradoroberto.it!diesel.cu.mi.it!bofh.it!news.nic.it!robomod
References: <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJRf-729Y-1@gated-at.bofh.it> <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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.corradoroberto.it!diesel.cu.mi.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 19:40:01 +0100
Message-ID: <NSxLX-83Fh-7@gated-at.bofh.it>
References: <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJRf-729Y-1@gated-at.bofh.it> <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>
X-Original-To: debian-user@lists.debian.org
X-Mailbox-Line: From debian-user-request@lists.debian.org  Mon Oct 26 18:38:15 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/832627
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/20261026183801.GB6334@wooledge.org
Approved: robomod@news.nic.it
Lines: 54
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Date: Mon, 26 Oct 2026 14:38:01 -0400
X-Original-Message-ID: <20261026183801.GB6334@wooledge.org>
X-Original-References: <20261021150743.GA21484@wooledge.org>
 <b397ac30-dbff-4822-b314-c6cc97dc0df8@gmail.com>
 <20261021190429.GA28455@wooledge.org>
 <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>