Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the Enter key is pressed in a linux bash script

Tags:

linux

bash

I want to check if the value entered by the user using the read command is the Enter key using if/elif statements in order to echo something. How should I do?

like image 754
Jafar Albadarneh Avatar asked Oct 19 '25 13:10

Jafar Albadarneh


1 Answers

The \n at the end of the user's input is stripped off before assigning the value to the shell variable. If the user just presses ENTER, the value read will be the empty string.

read VAR
if [[ -z $VAR ]]; then echo "User pressed ENTER with no input text"; fi
like image 82
Jim Garrison Avatar answered Oct 21 '25 02:10

Jim Garrison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!