Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash autocompletion with file names

I can't get a simple bash autocompletion function to work. I need to autocomplete file names from a predefined directory so it will look like this:

$ cmd log<TAB><TAB>
file1.log file2.log file3.log   

Where files are from /var/log/app.

like image 692
jackhab Avatar asked Mar 10 '26 04:03

jackhab


1 Answers

I don't see the point of using ls when the shell can list files just fine by itself, so here's one using just the shell.

_cmd() {
    local files=("/var/log/app/$2"*)
    [[ -e ${files[0]} ]] && COMPREPLY=( "${files[@]##*/}" )
}
complete -F _cmd cmd
like image 150
geirha Avatar answered Mar 11 '26 23:03

geirha