zsh-users
 help / color / mirror / code / Atom feed
* array with newlines preserved as literal text
@ 2021-02-02 19:16 Ray Andrews
  2021-02-02 19:26 ` Roman Perepelitsa
  2021-02-02 20:25 ` Bart Schaefer
  0 siblings, 2 replies; 14+ messages in thread
From: Ray Andrews @ 2021-02-02 19:16 UTC (permalink / raw)
  To: Zsh Users

I create some nasty output involving a grep search, save to to a 
variable, send it to a function which ends up calling 'eval' which 
receives the input and executes it like this:

     @ eval "$@"
     echo "\nYou (hard return)
     will\\nregret'\x0a'\nthe day you '\n' were born."

... I can save the output to a file perfectly:
     $ eval "$@" >! junk

     $ cat junk
     echo "\nYou (hard return)
     will\\nregret'\x0a'\nthe day you '\n' were born."   # Perfectly 
identical.

... and I can recapture that text from the file to a variable like this:

     $ typeset -a array1=( "${(q+)$(<junk)}" )

     $ print -l $array1
     $'    echo "\nYou (hard return)
     will\\nregret'\x0a'\nthe day you '\n' were born."'

... pretty good.  And there's probably a way of stripping off the outer: 
$'...'.
But if I try to capture the output to a variable directly:

     $ array2=$(eval "$@")

... the results are spectacularly  bad no matter how I dress it up with 
' ${(f)....}' or quotes  any other invocation I can think of. But 
surely  there's a way.   I understand newlines are hard to ignore.  
Various ideas would treat the text: ' \n ' as text alright but also 
ignore the 'real' newline that is invisible in an editor but must of 
course be there in the real string so everything would end up on one 
line.  Instead of using '(q+)' I tried '(F)' and stripped out the 
newlines like this: 'array2=${array2//'\n'/-NL-}' and it sorta works but 
it's vulgar.  The ' \x0a' is forgivable. Note that the input of all this 
is grep searches in code, so the lines found are bound to contain all 
sorts of rotten stuff, but I want literal output.
How do I pull this off?  There will be a way.



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2021-02-05  4:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-02 19:16 array with newlines preserved as literal text Ray Andrews
2021-02-02 19:26 ` Roman Perepelitsa
2021-02-03  1:10   ` Ray Andrews
2021-02-02 20:25 ` Bart Schaefer
2021-02-02 23:31   ` Ray Andrews
2021-02-03  2:21     ` Bart Schaefer
2021-02-03 19:29       ` Ray Andrews
2021-02-03 21:19         ` Bart Schaefer
2021-02-04  2:07           ` Ray Andrews
2021-02-05  2:57             ` Bart Schaefer
2021-02-04 21:55           ` Ray Andrews
2021-02-05  3:22             ` Bart Schaefer
2021-02-05  4:17               ` Ray Andrews
2021-02-05  4:24                 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).