How do I add a line break for read command?

前端 未结 7 629
孤街浪徒
孤街浪徒 2020-12-24 10:43
 read -p \"Please Enter a Message:\" message

How can I add a line break after Message:?

7条回答
  •  情话喂你
    2020-12-24 11:00

    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:

    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.

    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.

提交回复
热议问题