Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: protect spaces and/or globs



On 2021-02-09 5:52 p.m., Greg Klanderman wrote:

    It works Greg, but it misses the crux of my problem which is that all my wrappers run everything thru 'eval'
as below (simplified from your original):

grep_wrapper ()
{
  grepargs=()
  sstring=
  while [[ $# -gt 0 ]] ; do
    arg="$1"
    shift
      grepargs+=( "$arg" )                            # This is what Peter was saying.
  done
#  grep --color=always -i -- "${grepargs[@]}"         # Works fine but ...
  sstring="grep --color=always -i -- ${grepargs[@]}"  # I need to save to a scalar ...   print -rS "$sstring"                                # Write to history ...
  eval "$sstring"                                     # And 'eval'.

  print -r "\n$sstring"
}

9 /aWorking/Zsh/Source/Wk 4 $ . test; grep_wrapper 'on the current' 'i,2,light edit' 'i,1,old stable'
grep: the: No such file or directory
grep: current: No such file or directory
grep: i,2,light: No such file or directory
grep: edit: No such file or directory
grep: i,1,old: No such file or directory
grep: stable: No such file or directory

grep --color=always -i -- on the current i,2,light edit i,1,old stable

    ... as before the quotes are stripped if I do it my way.  But as I discovered last night:

9 /aWorking/Zsh/Source/Wk 4 $ . test; grep_wrapper "'on the current' 'i,2,light edit' 'i,1,old stable'"
i,2,light edit:Find any command named 'mtr*' on the current path.
i,1,old stable:Find any command named 'mtr*' on the current path.

grep --color=always -i -- 'on the current' 'i,2,light edit' 'i,1,old stable'

    ... by simply double quoting the argument string, the single quotes are preserved and 'eval' works.

She is NOT being unreasonable.   Just do it her way and everything will be fine.
A lesson for life.





Messages sorted by: Reverse Date, Date, Thread, Author