Bash Completion script to complete file path after certain arguments options
Alternatively if you do not have the bash-completion-lib, I looked at the source of the _filedir() function and was able to get full bash completion using comptopt -o
along with compgen -f
:
if [[ ${prev} == --*file ]] || [[ ${prev} == --out ]]; then
comptopt -o filenames 2>/dev/null
COMPREPLY=( $(compgen -f -- ${cur}) )
elif ...
However, you do not get the functionality of resolving ~usernames or case-insensitivity that _filedir() provides.
You can use compgen -f
to complete filenames, like this:
if [[ ${prev} == --*file ]] || [[ ${prev} == --out ]]; then
COMPREPLY=( $(compgen -f -- ${cur}) )
elif ...
However, compgen -f
isn't great at completing filenames because it doesn't honour spaces in filenames.
A better way is to use the _filedir
function available in bash-completion-lib. It might already be available on your system (type: declare -f _filedir
to check).
Using _filedir
, your completion function becomes:
if [[ ${prev} == --*file ]] || [[ ${prev} == --out ]]; then
_filedir
elif ...