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

Re: protect spaces and/or globs



> On 10 February 2021 at 15:18 Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>    sstring="grep --color=always -i -- ${grepargs[@]}"  # I need to save 
> to a scalar ...
>    print -rS "$sstring"                                # Write to 
> history ...
>    eval "$sstring"                                     # And 'eval'.

Don't turn it into a string here.

fullargs=(grep --color=always -i -- "${grepargs[@]}")
print -rS "$fullargs"
"${fullargs[@]}"

No eval needed, the arguments are now exactly the correct command arguments.

Actually, the

"${stuff[@]}"

is only needed to preserve empty arguments.  It's a good habit to get into,
but you can think about it more simply as just

$stuff

i.e. a set of array elements that turn into a set of command arguments.

pws




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