read -p "Please Enter a Message:" message
How can I add a line break after Message:
?
如何在消息后添加换行符:?
7 个解决方案
#1
28
I like Huang F. Lei's answer, but if you don't like the literal line break, this works:
我喜欢黄f雷的答案,但如果你不喜欢文字的断行,这是可行的:
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.
不过,必须有更好的办法。
#2
43
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 manpage解释它的摘录:
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:
格式$'string'的单词是专门处理的。单词扩展为字符串,由ANSI C标准指定的反斜线字符替换。反斜杠转义序列,如果存在,被解码如下:
- (...)
- (…)
- \n new line
- \ n新行
- (...)
- (…)
The expanded result is single-quoted, as if the dollar sign had not been present.
扩大的结果是单引号,好像美元符号没有出现。
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,美元符号将被忽略。如果字符串被翻译和替换,则替换为双引号。
#3
9
$ read -p "Please Enter a Message:
> " message
Please Enter a Message:
Typing a "newline" between ':' and '"' directly.
在“:”和“”之间输入“换行”。
#4
6
Just to improve the answers of Huang F. Lei and of T.J. Crowder which 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):
仅仅是为了改进我喜欢的黄F. Lei和T.J. 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。
#5
2
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:
所以,不是通过读取本身,并且在消息字符串中放入\n,只是回传了\n。答案应该是简单的-不要被阅读来显示提示:
echo "Please Enter a Message:" 1>&2
read message
#6
2
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:
从GNU 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.格式$'string'的单词是专门处理的。单词扩展为字符串,由ANSI C标准指定的反斜线字符替换。
#7
-2
read -p "Please Enter a Message:
Return" message
读-p“请输入一条信息:返回”信息。
#1
28
I like Huang F. Lei's answer, but if you don't like the literal line break, this works:
我喜欢黄f雷的答案,但如果你不喜欢文字的断行,这是可行的:
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.
不过,必须有更好的办法。
#2
43
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 manpage解释它的摘录:
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:
格式$'string'的单词是专门处理的。单词扩展为字符串,由ANSI C标准指定的反斜线字符替换。反斜杠转义序列,如果存在,被解码如下:
- (...)
- (…)
- \n new line
- \ n新行
- (...)
- (…)
The expanded result is single-quoted, as if the dollar sign had not been present.
扩大的结果是单引号,好像美元符号没有出现。
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,美元符号将被忽略。如果字符串被翻译和替换,则替换为双引号。
#3
9
$ read -p "Please Enter a Message:
> " message
Please Enter a Message:
Typing a "newline" between ':' and '"' directly.
在“:”和“”之间输入“换行”。
#4
6
Just to improve the answers of Huang F. Lei and of T.J. Crowder which 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):
仅仅是为了改进我喜欢的黄F. Lei和T.J. 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。
#5
2
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:
所以,不是通过读取本身,并且在消息字符串中放入\n,只是回传了\n。答案应该是简单的-不要被阅读来显示提示:
echo "Please Enter a Message:" 1>&2
read message
#6
2
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:
从GNU 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.格式$'string'的单词是专门处理的。单词扩展为字符串,由ANSI C标准指定的反斜线字符替换。
#7
-2
read -p "Please Enter a Message:
Return" message
读-p“请输入一条信息:返回”信息。