Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zsh read output of command into array splitting on newline

Tags:

zsh

I have a command that outputs a bunch of stuff e.g. running mycmd will give:

foobar
derp derp
like so
etc

Some of these lines will have spaces in them.

How do I read these into an array in zsh such that ${arr[1]} gives foobar, ${arr[2]} gives derp derp etc.

I have tried something like but it seems to split the array on chars not newlines.

IFS=$'\n' read -d '' -r arr <<< "$(mycmd)"

i.e. ${arr[1]} gives f when it should give foobar

like image 397
Ross MacArthur Avatar asked Aug 25 '17 09:08

Ross MacArthur


1 Answers

Okay its actually very simple:

IFS=$'\n' arr=($(mycmd))
like image 176
Ross MacArthur Avatar answered Sep 29 '22 12:09

Ross MacArthur