Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for no input in bash read?

Tags:

bash

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

like image 742
qliq Avatar asked Nov 06 '11 22:11

qliq


People also ask

How do you check if a variable is empty in Bash?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

How do I check if a string is empty in Bash?

To check if string is empty in Bash, we can make an equality check with the given string and empty string using string equal-to = operator, or use -z string operator to check if the size of given string operand is zero.

What is $0 in Bash script?

If the $0 special variable is used within a Bash script, it can be used to print its name and if it is used directly within the terminal, it can be used to display the name of the current shell.


1 Answers

unset input
while [ -z ${input} ]; do
     read input
done
like image 147
Mark L Avatar answered Nov 15 '22 22:11

Mark L