688 B
688 B
categories | tags | ||
---|---|---|---|
|
|
Splitting input into an array
readarray
readarray
makes it really easy to split input into an array.
Say we have this file as input:
123
456
789
Then we can split like so:
readarray -t name_for_array < ./input.text
# Print all elements
echo "${name_for_array[@]}"
# Print element by index
echo "${name_for_array[1]}"
456
If we want to read direct from string within bash file:
readarray -t new_name_for_array <<< "here
is
some
text"
echo "${new_name_for_array[1]}"
is
The -t flag removes the trailing newline