Tab completion in a BASH script

Discussion in 'Command Line' started by Kryyll, Mar 16, 2014.

  1. Kryyll

    Kryyll Active Member

    Dec 9, 2013
    Likes Received:
    I completely changed the way I was making a BASH script to decompress compressed files and I decided that I wanted to list all the compressed files in the current directory and then you can choose the one you want. However, it is a HUGE pain to have to enter in the entire file name. Is there anyway to enable tab completion within a script?

  2. MikeyD

    MikeyD Active Member

    Jul 23, 2013
    Likes Received:
    There is a minimally-documented command called "compgen" that expands an argument to generate completion matches. Unfortunately there is no man page for it and the --help option offers very little help (at least on SuSE 11). You can probably search the interwebz for more info, but using the -G option (glob pattern) seems to match info in the current directory:

    echo -n "Enter beginning of match: "
    read i
    x=`compgen -G "${i}*"`
    echo "$x"
    Usual wildcards apply so you can add another * to the beginning of the argument (${i}) to match files/folders with the string anywhere in the name.

    This is the closest thing to auto-complete that I know about in shell scripts

Share This Page