How to show read prompt with a new line

白昼怎懂夜的黑 提交于 2021-02-16 20:17:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!