Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Latest revision based on comment from <a href="https://stackoverflow.com/questions/11393817/bash-read-lines-in-file-into-an-array/11393884?noredirect=1#comment56571512_11393884">BinaryZebra's comment</a> and <a href="https://gist.github.com/nhed/d36ea500986d8fb6211c" rel="noreferrer">tested here</a>. The addition of <code>command eval</code> allows for the expression to be kept in the present execution environment while the expressions before are only held for the duration of the eval.</p> <p>Use $IFS that has no spaces\tabs, just newlines/CR</p> <pre><code>$ IFS=$'\r\n' GLOBIGNORE='*' command eval 'XYZ=($(cat /etc/passwd))' $ echo "${XYZ[5]}" sync:x:5:0:sync:/sbin:/bin/sync </code></pre> <p>Also note that you may be setting the array just fine but reading it wrong - be sure to use both double-quotes <code>""</code> and braces <code>{}</code> as in the example above</p> <hr> <p><strong>Edit:</strong></p> <p>Please note the many warnings about my answer in comments about possible glob expansion, specifically <a href="https://stackoverflow.com/questions/11393817/bash-read-lines-in-file-into-an-array/11393884?noredirect=1#comment35615660_11393884">gniourf-gniourf's comments</a> about my prior attempts to work around</p> <p>With all those warnings in mind I'm still leaving this answer here (yes, bash 4 has been out for many years but I recall that some macs only 2/3 years old have pre-4 as default shell)</p> <p><strong>Other notes:</strong></p> <p>Can also follow drizzt's suggestion below and replace a forked subshell+cat with </p> <pre><code>$(&lt;/etc/passwd) </code></pre> <p>The other option I sometimes use is just set IFS into XIFS, then restore after. See also <a href="https://stackoverflow.com/a/11394045/652904">Sorpigal's answer</a> which does not need to bother with this</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload