Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I assume that you have some variable that was constructed as a space separated list, and that your items may contain white spaces.</p> <p>This, as you can see, is awkward to iterate over in bash.</p> <p>If you have any control over the variable generation consider make it like this:</p> <pre><code>lines="a b c d" ( IFS=$'\n' ; for i in $lines ; do echo $i ; done ; ) </code></pre> <p>If you don't have control over the variable creation you can still make it a bit less convoluted:</p> <pre><code>eval array=($(echo "'a b' 'c d'")) for i in "${array[@]}" ; do echo $i ; done </code></pre> <hr> <p><strong>Update</strong> -- As commented below, some explanations are in order:</p> <p><em>1st solution:</em></p> <ul> <li><p>From man bash:</p> <blockquote> <p>IFS<br> The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is ``&lt;space&gt;&lt;tab&gt;&lt;newline&gt;''.</p> </blockquote></li> <li><p>Also note the somewhat convoluted way of assigning &lt;newline&gt; to IFS: <code>IFS=$'\n'</code></p></li> <li><p>Here we use <code>(</code> ... <code>)</code> to encapsulate the scope of IFS within a sub-shell. You can do without it if you don't care that the value of IFS will be changed, or if you devise an other way to restore it's original value, e.g. <code>saved_ifs=$IFS ; ... ; IFS=$saved_ifs</code></p></li> </ul> <p><em>2nd solution:</em></p> <ul> <li><p>We use <code>eval array=($(echo "'a b' 'c d'"))</code> and not <code>array=('a b' 'c d')</code>, because when we have <code>myvar="'a b' 'c d'"</code> (or any other combination I tried) <code>array=($myvar)</code> will not do the right thing (with or without quotes), but <code>eval array=($(echo $myvar))</code> will be evaluated to <code>array=('a b' 'c d')</code>.</p></li> <li><p>Note the difference between <code>"${array[@]}"</code> and <code>"${array[*]}"</code>; it is similar to the difference between "$@" and "$*" (again from man bash): </p> <blockquote> <p>* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.</p> <p>@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).</p> </blockquote></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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