Search code examples
bashshelltab-completion

Utilizing Bash Completion in a script


I have a script which takes a UUID as the argument. When I run it manually I can access it using custom tab completion I have written. Is there a way of accessing this completion in a shell script?

~/myscript stop 0011<tab>

It will complete to:

~/myscript stop 0011-1111-1111-1111

Edit: For example:

#!/bin/bash
echo "~/myscript stop \t" | bash -i 

The bash completion is already written. I'm currently executing it in another interactive shell, is there a way of executing this is the same shell?


Solution

  • I'm not sure why you can't just put the full UUID into your script. But I expect that you can do what you want to do using the bash compgen builtin.

    I haven't played with compgen much and I've never used custom tab completion, so I can't give you detailed instructions, but using compgen to generate a list of possible filename completions is easy enough.

    Eg, in my home directory,

    compgen -f .ba
    

    prints

    .bash_history
    .bashrc.dpkg-old
    .bashrc
    .bash_logout
    .bashrc~