Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

readarray names with whitespaces

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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 wan’t 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading