Can I implement tab autocomplete in a bash script?

5,250

Surprisingly (for me), read does simple file / folder autocomplete with the -e option.

To use it in your example (I've noted my changes)

echo  "Enter name of file to be tagged:"

cd ~/Downloads      ## cd to Downloads folder for autocomplete

read -e FileName

FileFindTest="$(find ~/Downloads/"$FileName")"  ## quoted 

echo "$FileFindTest"

In this case, when asking for input it will autocomplete files/folder in your Downloads folder.


Example, suppose you have the following files in your Downloads folder:

~/Downloads
│
├───Pop/
│   └───PopSong.mp3
├───Song1.mp3
└───Song5.mp3

Then when you will have the following results (pressing Tab when <TAB> is shown)

P<TAB>
Pop/

S<TAB>
Song1.mp3 Song5.mp3

PopSong.mp3<TAB>
# (nothing found here, as it's searching in Downloads/ only not Pop/)

Pop/P<TAB>
PopSong.mp3
Share:
5,250

Related videos on Youtube

Community
Author by

Community

Updated on September 18, 2022

Comments

  • Community
    Community almost 2 years

    I have a script that, when a user enters a file name in a directory, performs automatic tagging operations on that file (mp3 tagging).

    My problem: It gets really tedious typing in the exact file name of every song I want to tag, and I was wondering if it is possible to implement tab autocompletion when I input the name of a file.

    The beginning of my script:

    Input file name and locate file

    echo  "Enter name of file to be tagged"
    read -e FileName
    FileFindTest=$(find ~/Downloads/"$FileName")
    echo "$FileFindTest"