1

Why does this produces _results=""...

_results="$( grep ${_gopts[@]} )"

And this produces the desired _results (list of SSHFS entries in fstab)...

_results="$( eval grep ${_gopts[@]} )"

The _gopts array is identical in both cases and consists of...

declare -p _gopts
declare -a _gopts=([0]="--extended-regexp" [1]="--with-filename" [2]="--recursive" [3]="--include" [4]="fstab" [5]="'^[^#]*sshfs#'" [6]="/etc")
2

Because of the extra quotes around the 5th element (the regexp: [5]="'^[^#]*sshfs#'").

The grep command will be passed an argument of the form '^regex' instead of ^regex, which will not match, ever (there's no way for regex to both follow a single quote and start at the beginning of the line, at the same time).

Remove them, and then quote the array expansion ("${_gopts[@]}" instead of ${_gopts[@]}):

declare -p _gopts
declare -a _gopts=([0]="--extended-regexp" [1]="--with-filename" [2]="--recursive" [3]="--include" [4]="fstab" [5]="^[^#]*sshfs#" [6]="/etc")

_results=$( grep "${_gopts[@]}" )

You may also want to use single instead of double quotes in the array declaration: double quotes aren't necessary, since no element contains variables & other expansions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.