0

I need to compare a variable with a string (how hard can it be)

#!/bin/bash

a=22

if ["$a" == "23"]
then
    echo yes yes
fi

I get

./x: line 5: [22: command not found

I've tried to remove quotes, or use [[...]]. But whatever I do, I get the command not found error. Any suggestions what I do wrong?

Jeanluca Scaljeri
  • 19,619
  • 37
  • 147
  • 259
  • 1
    Well, that changes the error @Fabio. Now I get: `./x: line 3: local: can only be used in a function ./x: line 5: [: missing `]'` – Jeanluca Scaljeri Jun 22 '19 at 21:11
  • 1
    Spaces are important delimiters in shell syntax, and adding or removing them will often change the meaning of a command. – Gordon Davisson Jun 22 '19 at 22:08
  • 2
    notice that while the fundamental problem was the spacing, you should use `-eq` to compare integers instead of `==`. Furthermore, `==` is understood by Bash, but isn't POSIX compliant; use `=` instead for improved portability. – Benjamin W. Jun 22 '19 at 22:42

1 Answers1

1

[ "$a" == "22" ] <--- you need a space before the closing backet

gregory
  • 6,718
  • 2
  • 23
  • 33