13

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

2 Answers 2

13

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
2
  • 2
    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.
    – Idelic
    Commented Nov 7, 2011 at 7:01
  • The loop would stuck if there's no input.
    – davide
    Commented Sep 25, 2013 at 17:53
5
unset input
while [ -z ${input} ]; do
     read input
done

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.