Adding tab completion to a bash script

I am creating a script which does some processing on a file. For the file, Ι will select any file from the current directory or will give path to a file.

Problem is the file name can be huge, so typing it all will be a pain.

echo "Enter file name"
read FILE

The above code demonstrate what I am trying to do. When running this script, I have to put the file name character by character, which will be a huge pain. I want to get suggestions like in any terminal application by pressing Tab.

I want something like this Something like this to show up on pressing Tab while writing the file name. How can I achieve this?

Asked By: tycoon


You can read the input filename like this:

read -e -p "Enter filename: " f
printf "filename: %sn" "$f"

read -e uses readline which has already tab-completion for paths. read --help and man readline for details about them.

Answered By: thanasisp
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.