env : GNU bash, version 4.2.46(2)-release
list.txt
pineapple & pizza
mint & chocolate
broccoli & pizza
$ readarray -t cursed_food < list.txt
$ for food in ${cursed_food[@]}
> do
> echo ${food}
> done
expected outcome
pineapple & pizza
mint & chocolate
broccoli & pizza
the outcome
pineapple
&
pizza
mint
&
chocolate
broccoli
&
pizza
I googled for a while but it seems like my shell works differently than everyone else.
What am I doing wrong in here?
I really don't wan't to quote every lines of list.txt
.
What am I doing wrong in here?
You are not checking your script with shellcheck. If you would, you would know that, from https://www.shellcheck.net/ :
Line 1:
readarray -t cursed_food < list.txt
^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
Line 2:
for food in ${cursed_food[@]}
^-- SC2068 (error): Double quote array expansions to avoid re-splitting elements.
Line 4:
echo ${food}
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean: (apply this, apply all SC2086)
echo "${food}"
I really don't want to quote every lines of list.txt.
Changing input has nothing to do with how it is parsed. You have to change the way you use the variable, not the input.