15

I am looking for a conditional to avoid users from leaving an input value blank. Any suggestions?

2 Answers 2

14

No inputs (or even spaces I believe) get entered as empty strings, so check input while the input var is empty:

input=
while [[ $input = "" ]]; do
   read input
done
Sign up to request clarification or add additional context in comments.

2 Comments

This has the (minor) problem of not handling ^D as end-of-input, so it will loop forever if it reads from /dev/null. That can be dealt with if you write it as while read input && [ -z "$input" ]; do :; done.
The loop would stuck if there's no input.
5
unset input
while [ -z ${input} ]; do
     read input
done

Comments

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.