bash: Read lines in file into an array -
bash: Read lines in file into an array -
this question has reply here:
creating array text file in bash 2 answersi trying read file containing lines, bash array.
i have tried next far:
attempt1a=( $( cat /path/to/filename ) )
attempt2 index=0 while read line ; myarray[$index]="$line" index=$(($index+1)) done < /path/to/filename
both effort fail, in homecoming 1 element array containing first line of file. doing wrong?
i running bash 4.1.5
latest revision based on comment binaryzebra's comment , tested here. add-on of command eval
allows look kept in nowadays execution environment while expressions before held duration of eval.
use $ifs has no spaces\tabs, newlines/cr
$ ifs=$'\r\n' globignore='*' command eval 'xyz=($(cat /etc/passwd))' $ echo "${xyz[5]}" sync:x:5:0:sync:/sbin:/bin/sync
also note may setting array fine reading wrong - sure utilize both double-quotes ""
, braces {}
in illustration above
edit:
please note many warnings reply in comments possible glob expansion, gniourf-gniourf's comments prior attempts work around
with warnings in mind i'm still leaving reply here (yes, bash 4 has been out many years recall macs 2/3 years old have pre-4 default shell)
other notes:
can follow drizzt's suggestion below , replace forked subshell+cat
$(</etc/passwd)
the other alternative utilize set ifs xifs, restore after. see sorpigal's answer not need bother this
bash
Comments
Post a Comment