read 

Send to Kindle
home » snippets » zsh » read



Snippets

Read lines in file into an array

From: http://stackoverflow.com/a/11393884/2820637

IFS=$'\r\n' LINES=($(cat /etc/passwd))

Set array to a set of lines

# To test that $A doesn't get expanded
A=VAR_A

# -r/raw mode: to ignore the backslashes, etc.
# -d '': No delimiter.  Read until the end of the stream.
# -A lines:  Read into the array variable "lines".
IFS=$'\n' read -r -d '' -A lines <<"EOF"
a b c
$A 'bc'
$'\n'
'"\
EOF

print_args $lines

When run,

Received 4 args
1: 'a b c'
2: "$A 'bc'"
3: "$'\\n'"
4: '\'"\\'