help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] What is the different between `unset a[n-1]` in interact


From: Peng Yu
Subject: Re: [Help-bash] What is the different between `unset a[n-1]` in interactive mode and non-interactive mode?
Date: Tue, 18 Dec 2018 12:13:33 -0600

OK. This is the definitive explanation on why Bash behaves in the ways
as shown in the example. I haven't used [ often so I didn't recognize
that pattern matching was actually performed.

"Bash scans each word for the characters ‘*’, ‘?’, and ‘[’. If one of
these characters appears, then the word is regarded as a pattern, and
replaced with an alphabetically sorted list of filenames matching the
pattern (see Pattern Matching)."

On Tue, Dec 18, 2018 at 11:31 AM konsolebox <address@hidden> wrote:
>
> On Tue, Dec 18, 2018 at 9:56 AM Peng Yu <address@hidden> wrote:
> >
> > Hi,
> >
> > I can remove an array element in the following script.
> >
> > $ cat main.sh
> > #!/usr/bin/env bash
> > # vim: set noexpandtab tabstop=2:
> >
> > set -v
> > array=(a b c)
> > n=3
> > unset array[n-1]
> > declare -p array
> > $ ./main.sh
> > array=(a b c)
> > n=3
> > unset array[n-1]
> > declare -p array
> > declare -a array=([0]="a" [1]="b")
> >
> >
> > But when I run the same commands in the command line, it does not
> > delete the element.
> >
> > $ array=(a b c)
> > $ n=3
> > $ unset array[n-1]
> > $ declare -p array
> > declare -a array=([0]="a" [1]="b" [2]="c")
> >
> > However, if I quote it, then the element will be deleted.
> > $ unset 'array[n-1]'
> > $ declare -p array
> > declare -a array=([0]="a" [1]="b")
> >
> > Why is there such a difference between the interactive mode and the
> > non-interactive mode? Thanks.
> >
> > --
> > Regards,
> > Peng
> >
>
> https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
>
> And use failglob.
>
> --
> konsolebox



-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]