[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: String replacement drops leading '-e' if replacing char is a space
From: |
Greg Wooledge |
Subject: |
Re: String replacement drops leading '-e' if replacing char is a space |
Date: |
Mon, 14 Aug 2023 07:08:55 -0400 |
On Mon, Aug 14, 2023 at 06:40:28AM +0100, Kerin Millar wrote:
> On Mon, 14 Aug 2023 02:11:27 +0000
> pphick via Bug reports for the GNU Bourne Again SHell <bug-bash@gnu.org>
> wrote:
>
> > If a string starts with '-e' the replacement operators ${x//,/ } and ${x/,
> > /} drop the '-e'.
> > The behaviour seems to be very specific: the string must start with '-e'
> > and the replacing character has to be a space.
> >
> > Repeat-By:
> >
> > x='-e,b,c'
> > echo ${x//,/ }
> > b c
> > echo ${x/,/ }
> > b,c
>
> This is to be expected. Given that you haven't quoted the expansion, word
> splitting occurs, after which echo is run with three arguments. The first of
> these arguments is -e, which is treated as an option.
In addition to that, when the expansion is unquoted, IFS matters.
If the IFS variable is set to a non-default value, word splitting is
done *differently*, and the results could be pretty much anything.
Above and beyond all of that, it appears the OP is attempting to store
a list of values in a variable. The proper way to do that is with an
array variable, not a string variable.
unicorn:~$ x=(-e b c)
unicorn:~$ printf '<%s> ' "${x[@]}"; echo
<-e> <b> <c>
unicorn:~$ x=('first value' 'second value' 'third')
unicorn:~$ printf '<%s> ' "${x[@]}"; echo
<first value> <second value> <third>