4

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?

1
  • i've tried to check "\n" if entered but it did'nt work Commented Nov 25, 2013 at 18:44

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Also it is worth unsetting $IFS variable. Without it the code above can run on spaces not just \n.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.