在 Bash 脚本中调用“读取”时获取自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4819819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:21:08  来源:igfitidea点击:

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

bashbash-completion

提问by Ingo Fischer

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

在我的 Bash 脚本中,我正在读取用户输入的一些变量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?

现在我希望用户在必须进入目录时获得自动完成功能,就像您在 Bash shell 上一样。因此,当他输入目录的第一个字母时,他可以通过按 TAB 来自动完成它。那可能吗?

回答by miku

Try:

尝试:

read -e -p "Glassfish Path:" GF_DIR

-eenables readline:

-e启用阅读线

 -e 
    If the standard input is coming from a terminal, Readline is used
    to obtain the line.