7

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
vahid abdi
  • 9,636
  • 4
  • 29
  • 35
intelinside
  • 415
  • 1
  • 6
  • 6

3 Answers3

7

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

Secondly, check that $var equals "".

Bruno
  • 119,590
  • 31
  • 270
  • 376
4

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
pkout
  • 6,430
  • 2
  • 45
  • 55
0

try this:

read var

echo $REPLY|hexdump -C
vahid abdi
  • 9,636
  • 4
  • 29
  • 35
Anton
  • 105
  • 1
  • 3
  • 7