在Bash中预先填写提示 [英] Pre-filling a prompt in Bash
问题描述
写一个bash脚本,我想得到用户输入.太棒了,
Writing a bash script, and I want to get user input. Awesome,
read -p "What directory should we save in? " -e FOLDER
除了我想做的,理想,是让用户看到类似以下内容的东西:
Except that what I'd like to do, ideally, is have the user see something like:
What directory should we save in? /home/user/default/
,光标位于行尾,并具有向后删除或追加或其他功能.本质上,是预先填充用户的输入,但使他们能够对其进行编辑.
with the cursor at the end of the line, and the ability to delete backwards or append or whatever. Essentially, pre-filling the user's input, but giving them the ability to edit it.
Readline显然具有此功能,但是它似乎没有在read
命令中公开.还有其他选择吗?我希望不必使用perl或类似的东西.
Readline obviously has the capability, but it appears to be not exposed in the read
command. Any alternatives? I'd prefer to not have to use perl or such.
我要处理的约束是我正在编写一个易于广泛传播的shell脚本,因此应尽可能少地使用现有的基础结构. rlwrap
和read -i
都可以工作,如果它们的依赖项(分别为rlwrap
和bash版本>>)可用,则 可用.两者都是好的答案,请选择最适合您的.
The constraint I'm working under is that I'm writing a single shell script that would be nice to disseminate widely, so should rely on as little pre-existing infrastructure as possible. rlwrap
and read -i
both work if their dependencies (rlwrap
and bash version >> whatever I have, respectively) are available. Both good answers, choose whichever works for you.
推荐答案
You can wrap the command in rlwrap, which provides instant readline capabilities: http://utopia.knoware.nl/~hlub/rlwrap/#rlwrap
(rlwrap -P完成您想要的操作)
(rlwrap -P does what you want)
就3.2行(我想您正在使用的)而言,纯bash解决方案是可行的,我不认为
As far as a pure bash solution is concerned for the 3.2 line (which i am presuming you are using), I dont think its possible
这篇关于在Bash中预先填写提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!