Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: how to get verbatim output in command substitution



On Tue, Mar 25, 2008 at 08:13:45PM +0530, Sharad Pratap wrote:
[...]
> I wanted to use command substitution to assign to $mailpath array.
> but both of   `...`  and $(...)   , failed to do so.
> I have checked about the usage of command substitution in section
> 5.1.5: Backquotes at http://zsh.sunsite.dk/Guide/zshguide05.html
[...]

in list contexts, command substitution is split according to the
$IFS special parameter.

IFS=$'\n'
lines=($(cmd))

$lines[<n>] will contain the <n>th non-empty line in cmd's
output.

To include empty lines:

IFS=$'\n\n'
output=$(cmd; echo .)
output=${output%?}
lines=(${=output%$'\n'})

We need this because $(...) removes all the trailing NL
characters, while we only want to remove the one for the last
line.

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author