如何使用 read -p 命令启用 '\n' 解释?
How might one enable '\n' interpretation with read -p command?
以下代码提示后不显示换行符:
function renderOrNot {
read -n 1 -p "Press 'q' to exit or any other key to repeat rendering.\n" response
if [[ $response == "q" ]] ; then
exit
else
nice -19 prependRpollen.py && nice -19 template.sh && nice -19 raco pollen render *.html.p*
renderOrNot
fi }
nice -19 prependRpollen.py && nice -19 template.sh && nice -19 raco pollen render *.html.p*
renderOrNot
为了启用反斜杠解释,我不得不求助于使用 echo -e 来显示我的提示,如下所示:
function renderOrNot {
echo -e "Press 'q' to exit or any other key to repeat rendering.\n"
read -n 1 response
是否可以在read -p命令中完成反斜杠解释?
read -p "prompt"
不解释提示字符串的转义。
尽管字符串文字可以通过转义来表达控制字符,但如果它使用 POSIX 候选 ANSI-C 样式字符串语法 $'I am an ANSI-C style string\nin a shell script\n'
这种类型的字符串可用于 Bash read -p
提示字符串,如:
read -n 1 -p $'Press \'q\' to exit or any other key to repeat rendering.\n' response
作为 ANSI-C 样式字符串的替代方法,您可以将换行符直接添加到字符串中,如:
read -n 1 -p "Press 'q' to exit or any other key to repeat rendering.
" response
另请注意,read -p
提示和 read -n <integer>
要阅读的字符数是 Bash 特定功能,以及 echo -e
具有 echo
解释字符串文字的转义。
以下代码提示后不显示换行符:
function renderOrNot {
read -n 1 -p "Press 'q' to exit or any other key to repeat rendering.\n" response
if [[ $response == "q" ]] ; then
exit
else
nice -19 prependRpollen.py && nice -19 template.sh && nice -19 raco pollen render *.html.p*
renderOrNot
fi }
nice -19 prependRpollen.py && nice -19 template.sh && nice -19 raco pollen render *.html.p*
renderOrNot
为了启用反斜杠解释,我不得不求助于使用 echo -e 来显示我的提示,如下所示:
function renderOrNot {
echo -e "Press 'q' to exit or any other key to repeat rendering.\n"
read -n 1 response
是否可以在read -p命令中完成反斜杠解释?
read -p "prompt"
不解释提示字符串的转义。
尽管字符串文字可以通过转义来表达控制字符,但如果它使用 POSIX 候选 ANSI-C 样式字符串语法 $'I am an ANSI-C style string\nin a shell script\n'
这种类型的字符串可用于 Bash read -p
提示字符串,如:
read -n 1 -p $'Press \'q\' to exit or any other key to repeat rendering.\n' response
作为 ANSI-C 样式字符串的替代方法,您可以将换行符直接添加到字符串中,如:
read -n 1 -p "Press 'q' to exit or any other key to repeat rendering.
" response
另请注意,read -p
提示和 read -n <integer>
要阅读的字符数是 Bash 特定功能,以及 echo -e
具有 echo
解释字符串文字的转义。