问题
I'm using read
builtin to read a variable, but I'd like to let the input appears on the next line, that is, the prompt output a new line, but neither of the two works:
$ read -p "Please input:\n" name
Please input:\n
$ read -p 'Please input:\n" name
Please input:\n
As you see new line escape sequence is not interpreted even in the double quote case. So is there anyway to do that?
回答1:
You can separate the prompt from the actual read :
echo "Please input:"
read name
You can put both on a single line :
echo "Please input:" ; read name
You can also use a different form of quoting :
read -p $'Please input\n' name
This is barely shorter, and many would probably find it a bit less readable, but that is a matter of taste.
来源:https://stackoverflow.com/questions/43462279/how-to-show-read-prompt-with-a-new-line