Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, don't do it that way. The best approach is to use <code>find -exec</code> properly:</p> <pre><code># this is safe find test -type d -exec echo '{}' + </code></pre> <p>The other safe approach is to use NUL-terminated list, though this requires that your find support <code>-print0</code>:</p> <pre><code># this is safe while IFS= read -r -d '' n; do printf '%q\n' "$n" done &lt; &lt;(find test -mindepth 1 -type d -print0) </code></pre> <p>You can also populate an array from find, and pass that array later:</p> <pre><code># this is safe declare -a myarray while IFS= read -r -d '' n; do myarray+=( "$n" ) done &lt; &lt;(find test -mindepth 1 -type d -print0) printf '%q\n' "${myarray[@]}" # printf is an example; use it however you want </code></pre> <p>If your find doesn't support <code>-print0</code>, your result is then unsafe -- the below will not behave as desired if files exist containing newlines in their names (which, yes, is legal):</p> <pre><code># this is unsafe while IFS= read -r n; do printf '%q\n' "$n" done &lt; &lt;(find test -mindepth 1 -type d) </code></pre> <p>If one isn't going to use one of the above, a third approach (less efficient in terms of both time and memory usage, as it reads the entire output of the subprocess before doing word-splitting) is to use an <code>IFS</code> variable which doesn't contain the space character. Turn off globbing (<code>set -f</code>) to prevent strings containing glob characters such as <code>[]</code>, <code>*</code> or <code>?</code> from being expanded:</p> <pre><code># this is unsafe (but less unsafe than it would be without the following precautions) ( IFS=$'\n' # split only on newlines set -f # disable globbing for n in $(find test -mindepth 1 -type d); do printf '%q\n' "$n" done ) </code></pre> <p>Finally, for the command-line parameter case, you should be using arrays if your shell supports them (i.e. it's ksh, bash or zsh):</p> <pre><code># this is safe for d in "$@"; do printf '%s\n' "$d" done </code></pre> <p>will maintain separation. Note that the quoting (and the use of <code>$@</code> rather than <code>$*</code>) is important. Arrays can be populated in other ways as well, such as glob expressions:</p> <pre><code># this is safe entries=( test/* ) for d in "${entries[@]}"; do printf '%s\n' "$d" done </code></pre>
    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