You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This works exactly as I would expect.
But suppose I first want to replace all occurences of foo with bar before calling program, naturally I tried this:
program (string replace -a "foo" "bar" $variable)
Now it works most of the time, but not if $variable contains a newline, in which case it actually passes multiple arguments to foo, none of which contain newlines.
So I tried using string collect:
program (string replace -a "foo" "bar" $variable | string collect)
Which almost works! The newlines are preserved, unless$variableends in a newline, in which case it is striped.
So next I tried using the --no-trim-newlines option to string collect:
program (string replace -a "foo" "bar" $variable | string collect -N)
Unfortunately now program always gets an extra newline (since string replace itself keeps outputing one). I want the argument to program to end in a newline if and only if $variable does.
Eventuallly, I found a solution that almost works:
program (string replace -a "foo" "bar" $variable | head -c -1 | string collect -N)
Another solution is to use something other than string replace:
program (echo -n $variable | sed 's/foo/bar/g' | string collect -N)
Unfortunatly that doesn't work if $variable is the empty string, because then program get's no arguments. I also have to pass the --allow-empty option to string collect.
My point is that I think it would be very helpfull if string collect had an option that did this (and/or an option to string replace and other builtins to not print newlines I never asked for).
The text was updated successfully, but these errors were encountered:
Bassically, consider the following code:
This works exactly as I would expect.
But suppose I first want to replace all occurences of
foo
withbar
before callingprogram
, naturally I tried this:Now it works most of the time, but not if
$variable
contains a newline, in which case it actually passes multiple arguments tofoo
, none of which contain newlines.So I tried using
string collect
:Which almost works! The newlines are preserved, unless
$variable
ends in a newline, in which case it is striped.So next I tried using the
--no-trim-newlines
option tostring collect
:Unfortunately now
program
always gets an extra newline (sincestring replace
itself keeps outputing one). I want the argument toprogram
to end in a newline if and only if $variable does.Eventuallly, I found a solution that almost works:
Another solution is to use something other than
string replace
:Unfortunatly that doesn't work if
$variable
is the empty string, because thenprogram
get's no arguments. I also have to pass the--allow-empty
option tostring collect
.My point is that I think it would be very helpfull if
string collect
had an option that did this (and/or an option tostring replace
and other builtins to not print newlines I never asked for).The text was updated successfully, but these errors were encountered: