Re: String replacement drops leading '-e' if replacing char is a space

2023-08-14 Thread Greg Wooledge
On Mon, Aug 14, 2023 at 06:40:28AM +0100, Kerin Millar wrote:
> On Mon, 14 Aug 2023 02:11:27 +
> pphick via Bug reports for the GNU Bourne Again SHell  
> 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>   
unicorn:~$ x=('first value' 'second value' 'third')
unicorn:~$ printf '<%s> ' "${x[@]}"; echo
   



Re: String replacement drops leading '-e' if replacing char is a space

2023-08-14 Thread Zachary Santer
On Mon, Aug 14, 2023 at 1:27 AM Eduardo Bustamante 
wrote:

> The echo command is consuming the '-e', as it is a flag.  Instead, try
> using:
>
>   printf '%s\n' "${x/,/ }"
>

Or just redefine your echo, lol.

echo() {
  local IFS=' '
  printf '%s\n' "${*}"
}

Nah, just don't ever use echo for printing variables. That's just a rule of
thumb that everyone needs to know.


Re: ! history expansion occurs within arithmetic substitutions

2023-08-14 Thread Chet Ramey

On 8/11/23 4:19 PM, Martin D Kealey wrote:

On Sat, 12 Aug 2023, 02:29 Dale R. Worley,  wrote:


… The line is broken into  words in the same fashion as when reading
input, so that several metacharacter-separated words surrounded by quotes
are considered one word. …



I think it would be helpful to start this sentence with "The selected
line…", to differentiate from the line just entered containing a "!".


It's both, actually. The line being expanded is broken into words to bound
the history expansion, since a history expansion doesn't extend to multiple
words, and the event is broken into words for the rest of the process.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: !& error

2023-08-14 Thread Chet Ramey

On 8/12/23 1:56 PM, Grisha Levit wrote:

The newly added support for `! &' uses the previous value of
the_printed_command_except_trap or segfaults if none has been made yet:


Thanks for the report. You're Johnny on the spot here -- I hadn't started
writing tests for this yet.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/