Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a line break for read command?

Tags:

bash

shell

unix

 read -p "Please Enter a Message:" message 

How can I add a line break after Message:?

like image 313
Strawberry Avatar asked Nov 28 '10 09:11

Strawberry


People also ask

How do you add a line break in Linux?

The most used newline character If you don't want to use echo repeatedly to create new lines in your shell script, then you can use the \n character. The \n is a newline character for Unix-based systems; it helps to push the commands that come after it onto a new line.

How do I break a line in bash?

Linux Files, Users, and Shell Customization with Bash If you want to break up a command so that it fits on more than one line, use a backslash (\) as the last character on the line. Bash will print the continuation prompt, usually a >, to indicate that this is a continuation of the previous line.

How do you echo a new line?

There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.


2 Answers

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.

like image 54
ferhtgoldaraz Avatar answered Sep 19 '22 23:09

ferhtgoldaraz


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.

like image 36
T.J. Crowder Avatar answered Sep 21 '22 23:09

T.J. Crowder