0

I know there are a lot of tutorials on bash completion, but I just can't figure this out.

All I want is this. If I type myscript[tab][tab], then "myscript list-commands" is run. It will output a space delimited list of available commands (but I can output it however is appropriate). That output list is used for tab completion.

What do I put it my .bashrc to make that happen?

2
  • The complete builtin can specify the type of autocompletion for a given function. For more info see help complete or man bash Commented Jun 2, 2019 at 19:42
  • That's close to what I want. When I asked the original question, I didn't realize that completion works not just for the first option but for all subsequent options. Here's what I have so far which isn't quite what I want: complete -C "myscript list-commands" myscript That means that if I type in a command like this: myscript [tab][tab] I get a list of commands. Then if I complete one of the commands, then hit [tab][tab] again, I get the list of commands again. What I want is that second time I get a list of *.sql files in the current directory. How do I do that? Commented Jun 2, 2019 at 21:29

1 Answer 1

1

The easiest way is using a list of words/commands that your script supports:

Put the following into your .bashrc to have your script myscript support the commands add, list, delete:

complete -W "add list delete" myscript

This will leads to

> myscript [tab][tab]
add list delete

Hope this help. For further, more dynamic options than a simple wordlist have a look at the manpage of the complete command: https://ss64.com/osx/complete.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.