🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.debian.user
Article #311

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

#311
From: Greg Wooledge
Date: Tue, 27 Oct 2026 12:10
80 lines
5078 bytes
On Tue, Oct 27, 2026 at 07:38:01 +0100, john doe wrote:
> Now that  I have publicly humiliated myself, lets make it a learning
> moment!!! ;^)
> 
> What would be the correct way to populate an array programmatically?

Depends on what you're putting in it.  If it's just a fixed set of
strings, simply write them out:

    a=( zero one two three )
    b=( {0..9} )
    c=(
        "The first element is a sentence."
        "So is the second element."
        "I'm not."              # ???
        "Yes, you are!"
    )
    # Yes, you can put comments inside this format.

If it's filenames, use a glob (pathname expansion):

    d=( ~/wallpaper/* )

Remember that the glob characters, and in this case the tilde, are
supposed to be expanded by the shell, so they must not be quoted.  If
there's a separate part of the word that needs to be quoted, then
put the quotes only around that part:

    e=( "$cfgdir"/*.conf )

If the array is populated by the output of a program, then use the
readarray (a.k.a. mapfile) builtin, or a loop:

    readarray -t f < <(something that writes one element per line)

    readarray -d '' -t g < <(something that writes NUL-delimited elements)

    # This is what we used before readarray -d ''
    h=()
    while LC_ALL=C IFS= read -r -d '' file; do
        h+=( "$file" )
    done < <(find ... -print0)

*Do not* use this with a naive ls command.

    # Don't do this!
    readarray -t wrong < <(ls *.gif)

ls will often mangle filenames, and even if it doesn't, filenames that
contain newlines will break this.  If you want to load the filenames
from a directory non-recursively, use the glob syntax:

    i=( *.gif )

If you need to grab them recursively, use find with -print0:

    readarray -d '' -t j < <(find . -type f -name '*.gif' -print0)

Or you can use bash's globstar option:

    shopt -s globstar
    k=( **/*.gif )

If you need to get the filenames in an order that isn't what globbing
produces (e.g. sorted by mtime), GNU coreutils ls has special options
that can make this work.  I cover this at
<https://mywiki.wooledge.org/ParsingLs#Notes_on_GNU_coreutils_ls> and
I'll show the first example here:

    # Delete the 5 oldest files
    # in the current directory.
    readarray -t -d '' -n 5 sorted < <(ls --zero -tr)
    (( ${#sorted[@]} == 0 )) || rm -- "${sorted[@]}"

If your ls command doesn't have a --zero option, or a --quoting-style
option, then you can't use it to grab filenames programmatically.
GNU find with a fancy -printf option would be your next recourse.
There's an example on <https://mywiki.wooledge.org/BashProgramming/03>
(search for rlart) but it doesn't use arrays, and it's a bit beyond the
complexity limit for this email.
Technical Details
Message-ID: <NSOa5-8ev4-5@gated-at.bofh.it>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!2.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!bofh.it!news.nic.it!robomod
References: <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> <NSBmx-85yG-13@gated-at.bofh.it> <NSOa5-8ev4-7@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!2.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!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: Tue, 27 Oct 2026 13:10:01 +0100
Message-ID: <NSOa5-8ev4-5@gated-at.bofh.it>
References: <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> <NSBmx-85yG-13@gated-at.bofh.it> <NSOa5-8ev4-7@gated-at.bofh.it>
X-Original-To: debian-user@lists.debian.org
X-Mailbox-Line: From debian-user-request@lists.debian.org  Tue Oct 27 12:09:38 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/832632
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/20261027120922.GD6334@wooledge.org
Approved: robomod@news.nic.it
Lines: 80
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Date: Tue, 27 Oct 2026 08:09:22 -0400
X-Original-Message-ID: <20261027120922.GD6334@wooledge.org>
X-Original-References: <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>
 <20261026221426.GC6334@wooledge.org>
 <69cc2b8e-b7eb-404c-ad3c-0c8b1db3cbb6@mail.com>