🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.debian.user
Article #246

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

#246
From: Greg Wooledge
Date: Thu, 22 Oct 2026 12:10
60 lines
3661 bytes
On Thu, Oct 22, 2026 at 11:23:35 +0200, Michel Verdier wrote:
> On 2026-10-21, Greg Wooledge wrote:
> 
> >     IFS=:
> >     [[ ":${x[*]}:" =~ ":$i:" ]] ...
> >     unset IFS
> 
> Would it be safer to restore IFS ?
> With something like
> 
>   OLDIFS=$IFS
>   IFS=:
>   ...
>   IFS=$OLDIFS
> 
> or using local if within a function

Using local is good, yes.  You could even wrap this in a function so
that local becomes available.

Both "unset IFS" and "IFS=$OLDIFS" make assumptions that are not going
to be true universally.  Mine assumes that IFS was originally in its
default state, and that you want to continue being in the default
state afterward.  Unsetting IFS is operationally equivalent to
setting IFS to its default value of $' \t\n' (just easier to type).

Yours assumes that IFS *had* a prior value, and had not been unset.
If IFS was unset beforehand, then IFS=$OLDIFS will assign the empty
string to IFS, which is very different from unsetting it.

So, both of those are potentially wrong, and using local within a
function (even if it's a lambda created just for this purpose) is
definitely the safest approach.

If you ever need to do this sort of thing in POSIX sh, where local is
not yet mandated, saving IFS will need two steps: you'll need to check
whether IFS is currently unset, as well as tracking its current value
(if any).  Then, for the restoration, you will once again need to check
whether you should unset IFS, or restore its previous value.  This
could be done using one variable, or two variables, but either way it
will require at least two separate steps.  This should work:

saveIFS() {
    if test "${IFS+set}"; then
        _ifs=$IFS
    else
        unset _ifs
    fi
}   

restoreIFS() {
    if test "${_ifs+set}"; then
        IFS=$_ifs
    else
        unset IFS
    fi
}

Obviously it can be squeezed into fewer lines, but I wrote it in this
style for clarity.
Technical Details
Message-ID: <NQZMl-7b7x-9@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> <NQXhv-79Iz-9@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 14:10:01 +0200
Message-ID: <NQZMl-7b7x-9@gated-at.bofh.it>
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it> <NQXhv-79Iz-9@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org  Thu Oct 22 12:03:16 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/832554
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/20261022120258.GD28455@wooledge.org
Approved: robomod@news.nic.it
Lines: 60
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Date: Thu, 22 Oct 2026 08:02:58 -0400
X-Original-Message-ID: <20261022120258.GD28455@wooledge.org>
X-Original-References: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>
 <20261021150743.GA21484@wooledge.org>
 <877bjaxf54.fsf@verdier.net>