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

Re: protect spaces and/or globs



>>>>> On February 9, 2021 Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:

> When a function argument will end up making
> an argument to grep, and the argument to grep must have single quotes, but the
> single
> quotes typed in the function argument are stripped off. What's the solution?

You've gone wrong thinking 'the argument to grep must have single
quotes'.  You need single quotes to get the filename into the
function/script as a single argument initially, but if you keep it in
an array value, you can then expand the array in the grep invocation
preserving the arguments using the "${array[@]}" syntax.

consider

grep_wrapper () {
  grepargs=()
  while [[ $# -gt 0 ]] ; do
    arg="$1"
    shift
    case $arg in
      --foo* )
          echo "do something else with $arg"
          ;;
      * )
          grepargs+=( "$arg" )
          ;;
    esac
  done
  grep --color=always -i -- "${grepargs[@]}"
}

based on 'unsetopt shwordsplit' and some other options, you can get
away without some of the double quoting and {}.  but if you want to be able
to preserve the empty string, "", as an argument you are pretty much
stuck needing double quotes.

Greg




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