Hmm... this is an interesting proposal. I would do it something like this:
First, parse the file path to get the text after the last slash
IFS='/' read -a filepath <<< '$string'
dirname=${filepath[${#filepath[@] - 1]}
Next, use find
to get all the immediate subdirectories directories in the current path and add them to the bash completion option. You can use the =~
operator in place of fuzzy search, as described in this answer
for i in 'find . -type d -maxdepth 1'; do
if [[ i =~ $dirname ]]; then
//add to bash completion option, unsure how to do this part
fi
done
However, note that =~
is a bash-only operator.