Bash: Check if enter was pressed

梦想的初衷 提交于 2019-12-07 13:55:09

问题


How can I check in Bash if the Enter key has been pressed? I'm using the read command:

read -p "Please press ENTER" var

回答1:


Firstly, check whether the exit status is normal ($? should be 0).

Secondly, check that $var equals "".




回答2:


You can also check the length of the $var variable after it was set by the read call. If it's 0, the user just hit enter without typing anything else:

read -p "Please press ENTER" var
if [ ${#var} -eq 0 ]; then
  echo "Enter was hit"
fi



回答3:


try this:

read var

echo $REPLY|hexdump -C


来源:https://stackoverflow.com/questions/10385782/bash-check-if-enter-was-pressed

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