What is the purpose of `-i` in `read` in bash? How do you use it?

huangapple go评论41阅读模式
英文:

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 &quot;Make this rsync backup session a dry run [Y/n]? &quot; -i &#39;--dry-run&#39; dry_run
echo &quot;$dry_run&quot;

...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 &quot;Make this rsync backup session a dry run [Y/n]? &quot; -i &#39;--dry-run&#39;

prints this prompt:

Make this rsync backup session a dry run [Y/n]? --dry-run

where the --dry-run part is editable.

huangapple
  • 本文由 发表于 2023年2月16日 12:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定