Find the answer to your Linux question:
Results 1 to 3 of 3
I am trying to have tab completion in user entry in my bash script. I got the basic tab completion, but I only want to to work for text files. ...
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    3

    Tab Complete in Bash Script

    I am trying to have tab completion in user entry in my bash script. I got the basic tab completion, but I only want to to work for text files. I figured out that if I run it in the current shell (i.e. run it with ". script"), it works perfectly. My only problem with this is that the variables need to auto-clear and I don't want the end user to have to type that (this is commissioned for a friend). My current code is:

    Code:
    #!/bin/bash
    echo 'Input name of file'
    bind 'TAB:dynamic-complete-history'
    rm .bash_history2 2>/dev/null
    PotentialFiles=$(ls -tr *.txt)
    for i in $PotentialFiles ; do
            echo "$i" >> .bash_history2
    done
    history -c
    history -r .bash_history2
    compgen -W "$PotentialFiles" FileChosen
    read -e FileChosen
    Some of it is a little weird, such as the custom history file, but ignore this. This is only a part of a larger script.

    Thank you in advance!

  2. #2
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    Quote Originally Posted by Jibe View Post
    I am trying to have tab completion in user entry in my bash script. I got the basic tab completion, but I only want to to work for text files. I figured out that if I run it in the current shell (i.e. run it with ". script"), it works perfectly. My only problem with this is that the variables need to auto-clear and I don't want the end user to have to type that (this is commissioned for a friend). My current code is:

    Code:
    #!/bin/bash
    echo 'Input name of file'
    bind 'TAB:dynamic-complete-history'
    rm .bash_history2 2>/dev/null
    PotentialFiles=$(ls -tr *.txt)
    for i in $PotentialFiles ; do
            echo "$i" >> .bash_history2
    done
    history -c
    history -r .bash_history2
    compgen -W "$PotentialFiles" FileChosen
    read -e FileChosen
    Some of it is a little weird, such as the custom history file, but ignore this. This is only a part of a larger script.

    Thank you in advance!
    It seems that you need to add one of this commands that sets key binding before doing the "bind" command:
    Code:
    set -o vi
    
    set -o emacs
    so if you have the code:
    Code:
    #!/bin/bash
    
    set -o vi
    echo -n 'Input name of file: '
    bind 'TAB:dynamic-complete-history'
    rm .bash_history2 2>/dev/null
    PotentialFiles="$(ls -tr *.txt)"
    for i in $PotentialFiles ; do
            echo "$i" >> .bash_history2
    done
    ls -1tr *.txt > .bash_history2
    history -c
    history -r .bash_history2
    compgen -W "$PotentialFiles" FileChosen
    read -e FileChosen

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    3
    It works! Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...