Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I made a shell script that reads input from user and kills a process. But in bash if the user presses Tab key then bash suggests some keyword or file names. My script however doesn't suggest anything.

How can I enable tab completion for my script ?

share|improve this question

1 Answer 1

The easy way is to install bash-completion (or whatever else your distribution calls it), write a completion script, and put it in /etc/bash_completion.d. A basic completion script looks like this:

have cancel &&
_cancel()
{
    local cur

    COMPREPLY=()
    _get_comp_words_by_ref cur

    COMPREPLY=( $( compgen -W "$( lpstat | cut -d' ' -f1 )" -- "$cur" ) )
} &&
complete -F _cancel cancel

This is for the cancel command from cups. Read about COMPREPLY and compgen in the manual for bash, and adapt it to your needs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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