read 

Send to Kindle
home » snippets » bash » read



Snippets

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

# Can't just "print_args $lines" like in zsh.
print_args "${lines[@]}"

When run,

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