英文:
What is the purpose of `-i` in `read` in bash? How do you use it?
问题
我认为这个:
```bash
read -p "Make this rsync backup session a dry run [Y/n]? " -i '--dry-run' dry_run
echo "$dry_run"
...如果我只是按 Enter 键来回复提示,会输出 --dry-run
作为它的 "默认值"。但实际上不会。它输出一个换行符。
-i
的目的是什么,它是如何工作的?
从 help read
:
-i text 使用TEXT作为Readline的初始文本
对于想要查看我是如何学习如何在Bash中提示用户的任何人: How do I read user input into a variable in Bash?
<details>
<summary>英文:</summary>
I thought this:
```bash
read -p "Make this rsync backup session a dry run [Y/n]? " -i '--dry-run' dry_run
echo "$dry_run"
...would output --dry-run
as its "default value" if I just hit <kbd>Enter</kbd> to reply to the prompt. But, it doesn't. It outputs a newline.
What is the purpose of -i
and how does it work?
From help read
:
-i text use TEXT as the initial text for Readline
For anyone who wants to see where I learned how to make a bash prompt to the user: How do I read user input into a variable in Bash?
答案1
得分: 5
The -i
选项仅与 -e
选项一起使用以启用使用 Readline,它会在提示上预填充其内容:
read -ep "使此 rsync 备份会话成为干跑 [Y/n]? " -i '--dry-run'
打印此提示:
使此 rsync 备份会话成为干跑 [Y/n]? --dry-run
其中 --dry-run
部分可编辑。
英文:
The -i
option only works together with the -e
option to enable using Readline, and it prefills its contents on the prompt:
read -ep "Make this rsync backup session a dry run [Y/n]? " -i '--dry-run'
prints this prompt:
Make this rsync backup session a dry run [Y/n]? --dry-run
where the --dry-run
part is editable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论