Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get autocompletion when invoking a "read" inside a Bash script

Inside my Bash script, I'm reading some variables entered by the user with read:

read -p "Glassfish Path:" GF_DIR 

Now I want that the user gets a autocompletion when he has to enter a directory, like when you are on the Bash shell. So when he enters the first letters of a directory, he can autocomplete it by hitting TAB. Is that possible?

like image 363
Wolkenarchitekt Avatar asked Jan 27 '11 17:01

Wolkenarchitekt


People also ask

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How does autocomplete work in Bash?

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence. [1] If multiple completions are possible, then [Tab] lists them all. Let's see how it works. Tab completion also works for variables and path names.


1 Answers

Try:

read -e -p "Glassfish Path:" GF_DIR 

-e enables readline:

 -e      If the standard input is coming from a terminal, Readline is used     to obtain the line. 
like image 161
miku Avatar answered Oct 16 '22 00:10

miku