intelinside
intelinside

Reputation: 455

Bash: Check if enter was pressed

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

Upvotes: 7

Views: 9492

Answers (3)

Anton
Anton

Reputation: 105

try this:

read var

echo $REPLY|hexdump -C

Upvotes: 0

pkout
pkout

Reputation: 6736

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

Upvotes: 4

Bruno
Bruno

Reputation: 122669

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

Secondly, check that $var equals "".

Upvotes: 8

Related Questions