Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break a line (add a newline) in read -p in Bash?

How could I break a line using \n in read -p?

For example

read -p "Please Enter the percent [30 between 100]\n The value is  Default = 80   :" scale_percent

I want \n to break a line but it doesn't work.

In echo I use -e and it breaks a line.

So I tried

read -ep 

to break the line but it didn't break the line. How can I do that?

And could you also please give me a good manual for read -p on the Internet because I couldn't find a nice one without a confusing description.

like image 266
Medya Gh Avatar asked Oct 05 '12 07:10

Medya Gh


People also ask

How do you insert a line break in bash?

Printing Newline in Bash Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.

How do I break a line in echo bash?

On the command line, press Shift + Enter to do the line break inside the string.

How do I skip to the next line in bash?

From the bash manual: The backslash character '\' may be used to remove any special meaning for the next character read and for line continuation.


1 Answers

You can do it this way:

read -p $'Please Enter the percent [30 between 100]\x0a The value is  Default = 80   :' scale_percent

we use above syntax to insert hex values, we insert \x0a which is the hexadecimal value of the newline character (LF). You can use same syntax above with echo to produce new lines, eg:

echo $'One line\x0asecond line'

This is a feature of BASH 2, it is documented here, the $'' is used to translate all escape sequences inside it for its ascii transcription. So we could obtain the same result of example above, this way:

echo $'One line\nsecond line'
like image 55
Nelson Avatar answered Sep 22 '22 06:09

Nelson