Search code examples
bashshellunix

How do I add a line break for read command?


 read -p "Please Enter a Message:" message

How can I add a line break after Message:?


Solution

  • 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.