🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.debian.user
Article #259

Re: Bash: check if a value is into an array

#259
From: Greg Wooledge
Date: Thu, 22 Oct 2026 17:10
41 lines
3634 bytes
On Thu, Oct 22, 2026 at 22:45:21 +0700, Max Nikulin wrote:
> On 22/10/2026 1:48 am, Nicolas George wrote:
> > By the way, it seems what you want is not just distinct elements but all
> > the elements too: a permutation. For that, there is a much more
> > efficient algorithm: swap the first element randomly with any of the
> > elements, then the second with any of the elements except the first,
> > etc.
> 
> shuf(1) or, as it is recommended there,
> (info "(coreutils) shuf invocation")
> Unless the real goal is to explore BASH arrays.

Harnessing shuf(1) to shuffle a list of filenames presents its own flavor
of challenge.  It's not trivial, because of the nature of filenames.
It's not exactly *hard*, but you do have to know what you're doing.
(shuf(1) is also non-portable, but since this is a Debian list, I guess
we can assume it will be available.)

If you don't want to puzzle it out yourself, here's a shuf-based
example.  This one requires bash 4.4 or higher (for readarray -d):

#!/bin/bash

cd /wherever || exit
readarray -t -d '' playlist < <( printf '%s\0' * | shuf -z )
for file in "${playlist[@]}"; do
    mpv -- "$file"
done

If you're targeting older versions of bash, you'd need a while loop to
load the array.

"But Greg," I hear you say, "why are you messing around with NUL bytes?
Why not just use newlines?"

Filenames may contain newlines.  NUL is the only safe delimiter for
filenames.  Any tool that processes filenames as input or output, or
which may be *expected* to be used in that way, needs to have a -z or -0
or --null option.  And your bash script needs to use that option, and
perform all the other steps necessary to accomodate hostile filenames,
or else it *will* break.
Technical Details
Message-ID: <NR4sG-7dW8-21@gated-at.bofh.it>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!3.eu.feeder.erje.net!feeder.erje.net!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJxT-71Ty-3@gated-at.bofh.it> <NR3wB-7dhY-3@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!3.eu.feeder.erje.net!feeder.erje.net!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: check if a value is into an array
Date: Thu, 22 Oct 2026 19:10:02 +0200
Message-ID: <NR4sG-7dW8-21@gated-at.bofh.it>
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJxT-71Ty-3@gated-at.bofh.it> <NR3wB-7dhY-3@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org  Thu Oct 22 17:08:36 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:hard: -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/832571
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/20261022170821.GG28455@wooledge.org
Approved: robomod@news.nic.it
Lines: 41
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Date: Thu, 22 Oct 2026 13:08:22 -0400
X-Original-Message-ID: <20261022170821.GG28455@wooledge.org>
X-Original-References: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>
 <20261021150743.GA21484@wooledge.org>
 <b397ac30-dbff-4822-b314-c6cc97dc0df8@gmail.com>
 <atkI_Agk3vuY5vlg@phare.normalesup.org>
 <2042bac5-a5ea-4f1b-8922-18ca56ac7206@gmail.com>