Sample Header Ad - 728x90

Split zsh array from subshell by linebreak

6 votes
1 answer
2967 views
I would like to instantiate a zsh array from a subshell myarray=($(somecommand) and I check if I received what I wanted with for element in $myarray ; do echo "===" ; echo $element ; echo "---" ; done By default I see that whitespaces (spaces and line breaks) are used to separate elements. I also found that I can use ${(s:-:)"$(somecommand)"} to separate by -, in which case spaces and line breaks do not break elements (i.e. array elements can contain line breaks). So far I fail to split at line breaks only. I.e. if somecommand returns Alice Bob Arthur C Clarke Neo Thomas Anderson I want my above for loop to print: === Alice --- === Bob --- === Arthur C Clarke --- === Neo --- === Thomas Anderson --- How do I achieve that? (And possibly pointers where to look that up in the manual.)
Asked by pseyfert (890 rep)
Aug 6, 2018, 09:19 AM
Last activity: May 20, 2025, 05:02 PM