Skip to main content
added 178 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

You need spaces around the ==. Otherwise, you are settingpassing the string "$choice"==1 itself to if rather than comparing $choice to 1. When [ receives a string, it will evaluate to true as long as the valuestring is non-empty:

$ foo="bar";  [ $foo ] && echo true
true
$ foo="";  [ "$foo" ] && echo true  ## echoes nothing

Outside the test brackets, that succeeds andin the absence of space, the variable would actually get assigned to if=1 is run. To illustrate:

$ f==3
$ echo $f
=3
$ [ 10==12 ] && echo yes
yes

As you can see above, f==3 sets the variable $f to =3. Since the assignment is successful

So, in your if block, what is being tested is a non-empty string which always evaluates to true and the if is executed. This would work:

if [ "$choice" == "1" ]
then
    baseinstall
fi

Also, = or == do string comparison in bash. You want arithmetic comparison:

if [ "$choice" -eq "1" ]
then
    baseinstall
fi

You need spaces around the ==. Otherwise, you are setting $choice to the value, that succeeds and the if is run. To illustrate:

$ f==3
$ echo $f
=3
$ [ 10==12 ] && echo yes
yes

As you can see above, f==3 sets the variable $f to =3. Since the assignment is successful, the if is executed. This would work:

if [ "$choice" == "1" ]
then
    baseinstall
fi

Also, = or == do string comparison in bash. You want arithmetic comparison:

if [ "$choice" -eq "1" ]
then
    baseinstall
fi

You need spaces around the ==. Otherwise, you are passing the string "$choice"==1 itself to if rather than comparing $choice to 1. When [ receives a string, it will evaluate to true as long as the string is non-empty:

$ foo="bar";  [ $foo ] && echo true
true
$ foo="";  [ "$foo" ] && echo true  ## echoes nothing

Outside the test brackets, in the absence of space, the variable would actually get assigned to =1. To illustrate:

$ f==3
$ echo $f
=3
$ [ 10==12 ] && echo yes
yes

As you can see above, f==3 sets the variable $f to =3.

So, in your if block, what is being tested is a non-empty string which always evaluates to true and the if is executed. This would work:

if [ "$choice" == "1" ]
then
    baseinstall
fi

Also, = or == do string comparison in bash. You want arithmetic comparison:

if [ "$choice" -eq "1" ]
then
    baseinstall
fi
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

You need spaces around the ==. Otherwise, you are setting $choice to the value, that succeeds and the if is run. To illustrate:

$ f==3
$ echo $f
=3
$ [ 10==12 ] && echo yes
yes

As you can see above, f==3 sets the variable $f to =3. Since the assignment is successful, the if is executed. This would work:

if [ "$choice" == "1" ]
then
    baseinstall
fi

Also, = or == do string comparison in bash. You want arithmetic comparison:

if [ "$choice" -eq "1" ]
then
    baseinstall
fi