Autocomplete file name in a bash script

Viewed 704

I'm trying to imitate the bash file completion.
Suppose I have the following files:

test1
test2

With an input string of "te" I would like to get the output "test"

This is my current attempt ($c is the input string):

l=1
q="$c"
for j in $(ls -A | grep "^$c"); do
    if [ "${j/$c}" != "$j" ]; then 
            n=$(ls -A | grep ^$j | wc -l)
            if [ $n -gt $l ]; then 
                    q="$j"  
            fi      
    fi
done
c="$q"
echo $c

Thanks for any help

3 Answers
Related