-1

I just do a simple task,input a bc then if c is "+" it will print total of a+b

So i do this

echo "$a $b $c = "

if [ $c = "+" ]; then
    echo $(($a + $b))
fi

And it return simple.sh: 3: [: =: unexpected operator

What is going on?? Please help, thank a lot, im so confuse now

Lê Quốc Khánh
  • 545
  • 1
  • 6
  • 20

1 Answers1

2

If $c is unset or set to nothing then the expression would expand to: [ = + ] which would cause this problem.

Always quote your parameter expansions as the result will be unexpected otherwise:

[ "$c" = "+" ]

Unquoted parameter expansions will undergo word splitting and pathname expansion.

Whenever writing shellscripts it always a good idea to use the shellcheck linter, an online version is available at https://www.shellcheck.net/

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123