If condition - else is never executed

In the following code snippet, 'if' part is always executed irrespective of whether I enter 'y' or 'n'. I am unable to figure out my mistake even after spending couple of hours. Can someone point out the mistake? Thank you!

echo "Do you want to delete?(y/n):"
read USERINPUT
echo $USERINPUT

if [ $USERINPUT="y" ]
then 
	echo "Deleted."
elif [ $USERINPUT="n" ]
then 
	echo "Not deleted."
fi	

Space are very important in shell scripts:

echo "Do you want to delete?(y/n):"
read USERINPUT
echo $USERINPUT

if [ "$USERINPUT" = "y" ]
then 
	echo "Deleted."
elif [ "$USERINPUT" = "n" ]
then 
	echo "Not deleted."
fi

The command: [ $USERINPUT="y" ] (assuming there are no whitespace characters in the expansion of $USERINPUT is a single argument that is not an empty string (which always evaluates to TRUE). With spaces you have a test for string equality. Note also the double quotes around the values read from your user. Without them, your script can break in unexpected ways if the user types in an empty line or types in a line containing more than one "word".

1 Like

Thanks a lot Don Cragun, for pointing out my mistake. It works now! :slight_smile: