Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'have' keyword for bash completion

Is have a keyword in bash? Or do bash completion scripts use a language that is not bash?

have gcc &&
_gcc()
{

It is common. See: grep "have .* &&" /etc/bash_completion.d/*

I could not find any information on the bash completion tutorials I've seen and I could not find any information in man bash. It's also difficult to google "have". Where do I find documentation on this?

I'm guessing it has to do with making sure that there gcc exists in the PATH?

edit: yes. /etc/bash_completion contains:

have()
{
    unset -v have
    # Completions for system administrator commands are installed as well in
    # case completion is attempted via `sudo command ...'.
    PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null &&
    have="yes"
}
like image 347
Xu Wang Avatar asked Oct 13 '12 16:10

Xu Wang


People also ask

What key is used for bash completion?

Bash completion is a bash function that allows you to auto complete commands or arguments by typing partially commands or arguments, then pressing the [Tab] key. This will help you when writing the bash command in terminal.

How can I tell if I have bash completion?

You can use the complete command with the -p option to get a list of all or specific completions.

Does bash have tab completion?

Bash completion is a functionality through which Bash helps users type their commands more quickly and easily. It does this by presenting possible options when users press the Tab key while typing a command.


1 Answers

have and _have are just two functions defined in the base bash_completion file. Between the two, they form a wrapper around the built-in type command to determine if a particular command/program available.

# This function checks whether we have a given program on the system.
#
_have()
{
    # Completions for system administrator commands are installed as well in
    # case completion is attempted via `sudo command ...'.
    PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin type $1 &>/dev/null
}

# Backwards compatibility for compat completions that use have().
# @deprecated should no longer be used; generally not needed with dynamically
#             loaded completions, and _have is suitable for runtime use.
have()
{
    unset -v have
    _have $1 && have=yes
}
like image 123
chepner Avatar answered Sep 29 '22 16:09

chepner