How do I add a line break for read command?

前端 未结 7 655
孤街浪徒
孤街浪徒 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:13

    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

提交回复
热议问题