When I use break
in an if loop in bash it tells me its not valid for bash, what can I use instead?
The use case is, the user is asked a question and if he answers 'no', the script should skip to the next section.
if [[ $ans1_1 = "y" ]]; then
fedoraDeps
elif [[ $ans1_1 = "n" ]]; then
break
else
echo "Answer 'y' or 'n' "
fi
I agree that case is probably best. But you should probably cast to upper/lower case either way. For the way you had it:
Or, if you wanted uppercase ${ans1_1^^}
For this example, I think it makes more sense to use
case
.From
man bash
:This sounds like an ideal case for the
select
command:if
statements are not "loops", so it doesn't make sense to break out of them. If you want one of your blocks to be a no-op, you can use the built-in:
command, which simply does nothing: