[Bash-completion-devel] RFC: function to check if an array contains something

Ville Skyttä ville.skytta at iki.fi
Sun Nov 22 22:53:48 UTC 2009


Hello,

Quite a few completions would and do benefit from checking if some argument 
was already given, not only checking if it was the previous one.  I think this 
is tedious enough to warrant a helper function for it, assuming I didn't miss 
a nice way to do it in bash.

So, how about the attached patch?

I'm not too happy about the magical "array" variable or COMP_WORDS if it isn't 
set behaviour, but I don't know of a clean enough way to pass an array as a 
function argument along with a bunch of other ones.  Rationale for echoing the 
index instead of the found word is that completions may want to check what 
follows/precedes the found thing which would need another loop if the found 
word was echoed instead, and getting the found word is easy enough by using 
the found index.

Some example use cases:

    _in_array foo &>/dev/null && ...

    if i=$( _in_array "--@(foo|bar)" "--quux*" ) ; then
        local foo="${COMP_WORDS[$i]}"
        ...
    fi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: _in_array.patch
Type: text/x-patch
Size: 931 bytes
Desc: not available
URL: <http://lists.alioth.debian.org/pipermail/bash-completion-devel/attachments/20091123/0dc0964d/attachment.bin>


More information about the Bash-completion-devel mailing list