Dev
Dev

Reputation: 35

Why does my bash script not print out an echo statement?

I have a function that reads the width and checks for any characters, it doesn't print out the error I want it to

echo "Enter width"
read width

v='^[0-9]+$'

function width() {
if ! [[ $width =~ $v ]] ; then
   echo "ERROR - INPUT A NUMBER" >&2; return width
fi
}

Upvotes: 1

Views: 267

Answers (1)

Maroun
Maroun

Reputation: 95948

You can't do what you're trying to. If you want to keep reading the input until some condition is met, you should do something like:

v='^[0-9]+$' 

read -p "Enter width: " width
while [[ ! $width =~ $v ]]
do
  read -p "Enter width: " width
done

Upvotes: 1

Related Questions