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?
-
i've tried to check "\n" if entered but it did'nt workJafar Albadarneh– Jafar Albadarneh2013-11-25 18:44:15 +00:00Commented Nov 25, 2013 at 18:44
Add a comment
|
1 Answer
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
1 Comment
tsds
Also it is worth unsetting $IFS variable. Without it the code above can run on spaces not just \n.