bash 如何为读取命令添加换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4296108/
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
How do I add a line break for read command?
提问by Strawberry
read -p "Please Enter a Message:" message
How can I add a line break after Message:
?
如何在之后添加换行符Message:
?
采纳答案by T.J. Crowder
I like Huang F. Lei's answer, but if you don't like the literal line break, this works:
我喜欢Huang F. Lei 的回答,但如果你不喜欢文字换行,这有效:
read -p "Please Enter a Message: `echo $'\n> '`" message
Shows:
节目:
Please Enter a Message: > _
...where _
is where the cursor ends up. Note that since trailing newlines are usually dropped during command substitution, I've included the >
afterward. But actually, your original question doesn't seem to want that prompt bit, so:
...哪里_
是光标结束的地方。请注意,由于尾随换行符通常在命令替换过程中被删除,因此我包含了后面的换行符>
。但实际上,你最初的问题似乎并不想要那个提示,所以:
# Get a carriage return into `cr` -- there *has* to be a better way to do this
cr=`echo $'\n.'`
cr=${cr%.}
# Use it
read -p "Please Enter a Message: $cr" message
Shows
节目
Please Enter a Message: _
There has to be a better way, though.
不过,必须有更好的方法。
回答by ferhtgoldaraz
Just looking for the exact same thing. You can use:
只是在寻找完全相同的东西。您可以使用:
# -r and -e options are unrelated to the answer.
read -rep $'Please Enter a Message:\n' message
And it will work exactly as asked:
它将完全按照要求工作:
Please enter a Message:
_
Here is an extract from the bash manpage explaining it:
这是 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.
$'string' 形式的词被特殊处理。单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。反斜杠转义序列(如果存在)按如下方式解码:
- (……)
- \n 换行
- (……)
扩展结果是单引号的,就好像美元符号不存在一样。
Took me a while to find out.
我花了一段时间才知道。
Note that single quotes and double quotes behave differently in this regard:
请注意,单引号和双引号在这方面的表现不同:
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.
以美元符号 ($) 开头的双引号字符串将使字符串根据当前区域设置进行翻译。如果当前语言环境是 C 或 POSIX,则忽略美元符号。如果字符串被翻译和替换,则替换是双引号。
回答by augurar
Here's an improvement on the accepted answer that doesn't require spawning a subshell:
这是对不需要生成子shell的已接受答案的改进:
read -p "Please Enter a Message:"$'\n' message
From the GNU Bash reference manual:
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.
表格
$'string'
中的词被特殊处理。该单词扩展为string,并按照 ANSI C 标准的规定替换反斜杠转义字符。
回答by Huang F. Lei
$ read -p "Please Enter a Message:
> " message
Please Enter a Message:
Typing a "newline" between ':' and '"' directly.
直接在 ':' 和 '"' 之间键入“换行符”。
回答by Luca Borrione
Just to improve the answers of Huang F. Leiand of T.J. Crowderwhich I like (and added +1) .. You can use one of the following syntaxes too, which basically are the same, it depends on your taste (I prefer the first one):
只是为了改进我喜欢的Huang F. Lei和TJ Crowder的答案(并添加了 +1).. 您也可以使用以下语法之一,它们基本上是相同的,这取决于您的口味(我更喜欢第一):
read -p "$(echo -e 'Please Enter a Message: \n\b')" message
read -p "`echo -e 'Please Enter a Message: \n\b'`" message
which both will produce the following output:
这两者都会产生以下输出:
Please Enter a Message:
_
where _ is the cursor.
In case you need a newline in any part of the string but the end, you can use \n
, for example
其中 _ 是光标。
如果您需要在字符串的任何部分但结尾处有一个换行符,您可以使用\n
,例如
read -p "`echo -e '\nPlease Enter\na Message: '`" message
will produce
会产生
.
Please Enter
a Message: _
where . is a blank first new line and _ is the cursor.
Only to add a final trailing newline you have to use \n\b
as in my first example
在哪里 。是一个空白的第一个新行,_ 是光标。
仅添加最后一个尾随换行符,您必须\n\b
像在我的第一个示例中一样使用
回答by Alnitak
From the bash
manpage:
从bash
联机帮助页:
-p prompt
Display prompt on standard error, without a trailing new-
line, before attempting to read any input. The prompt is
displayed only if input is coming from a terminal.
So, not with read
itself, and putting \n
in the message string just echoes \n
. The answer should be simple though - don't get read
to display the prompt:
所以,不是与read
它自己,而是放入\n
消息字符串只是 echo \n
。答案应该很简单 - 不要read
显示提示:
echo "Please Enter a Message:" 1>&2
read message
回答by Ruggero Turra
read -p "Please Enter a Message:
Return" message
read -p "Please Enter a Message:
Return" message