1

The following example shows that one can provide arguments to find via an array in more situations than one can via a string. Yet both objects contain the same information. Is there a way to understand this other than just to say "magic"?

> bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
> 
> set -f
> find . -not -name '.*' -not -name *[[:space:]]*
./z
./okay
> xstring=' . -not -name '.*' -not -name *[[:space:]]* '
> find ${xstring}
./z
./okay
> find $xstring
./z
./okay
> find . -name *$'\n'*
./newlineBEF
newlineAFT
> xstring=" . -name *$'\n'* "
> find ${xstring}
> xarray=( . -name *$'\n'* )
> find "${xarray[@]}"
./newlineBEF
newlineAFT
> 
> for foo in ${xstring}; do printf 'foo=%s\n' "${foo}"; done
foo=.
foo=-name
foo=*$'\n'*
> for foo in "${xarray[@]}"; do printf 'foo=%s\n' "${foo}"; done
foo=.
foo=-name
foo=*
*
>

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jacob Wegelin
  • 1,304
  • 11
  • 16
  • 1
    Enable `set -x` to see what the shell's seeing and try it all again. It'll be enlightening. – John Kugelman Oct 22 '19 at 01:06
  • 1
    The shell parses quotes and escapes before substituting variables, so with a plain string there's no way to control word splitting and wildcard expansion of the variable's contents. See [this question](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables), [this one](https://stackoverflow.com/questions/29527983/why-do-bash-parameter-expansions-cause-an-rsync-command-to-operate-differently), [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050), etc. – Gordon Davisson Oct 22 '19 at 03:22

0 Answers0