How do I add a line break for read command?

BashShellUnix

Bash Problem Overview


 read -p "Please Enter a Message:" message

How can I add a line break after Message:?

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

I like Huang F. Lei's answer, but if you don't like the literal line break, this works:

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.

Solution 3 - Bash

Here's an improvement on the accepted answer that doesn't require spawning a subshell:

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.

Solution 4 - Bash

$ read -p "Please Enter a Message:
> " message
Please Enter a Message:

Typing a "newline" between ':' and '"' directly.

Solution 5 - Bash

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):

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

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

Solution 6 - Bash

From the bash manpage:

-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:

echo "Please Enter a Message:" 1>&2
read message

Solution 7 - Bash

read -p "Please Enter a Message:Return" message

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStrawberryView Question on Stackoverflow
Solution 1 - BashferhtgoldarazView Answer on Stackoverflow
Solution 2 - BashT.J. CrowderView Answer on Stackoverflow
Solution 3 - BashaugurarView Answer on Stackoverflow
Solution 4 - BashHuang F. LeiView Answer on Stackoverflow
Solution 5 - BashLuca BorrioneView Answer on Stackoverflow
Solution 6 - BashAlnitakView Answer on Stackoverflow
Solution 7 - BashRuggero TurraView Answer on Stackoverflow