gpt4 book ai didi

bash - 如何为读取命令添加换行符?

转载 作者:行者123 更新时间:2023-11-29 08:40:55 26 4
gpt4 key购买 nike

 read -p "Please Enter a Message:" message

如何在 Message: 之后添加换行符?

最佳答案

只是在寻找完全相同的东西。您可以使用:

# -r and -e options are unrelated to the answer.
read -rep $'Please Enter a Message:\n' message

它会完全按照要求工作:

Please enter a Message:
_

这是 bash 联机帮助页的摘录,对其进行了解释:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

  • (...)
  • \n new line
  • (...)

The expanded result is single-quoted, as if the dollar sign had not been present.

我花了一段时间才发现。

请注意,单引号和双引号在这方面的行为不同:

A double-quoted string preceded by a dollar sign ($) will cause the string to be translated according to the current locale. If the cur- rent locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

关于bash - 如何为读取命令添加换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4296108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com