Tab completion in a BASH script

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

  1. Kryyll

    Kryyll Active Member

    Messages:
    111
    Likes Received:
    38
    Trophy Points:
    28
    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

    Messages:
    232
    Likes Received:
    111
    Trophy Points:
    43
    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:

    Code:
    #!/bin/bash
    
    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