1

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?

3
  • 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 ]'` Commented Jun 22, 2019 at 21:11
  • 1
    Spaces are important delimiters in shell syntax, and adding or removing them will often change the meaning of a command. Commented Jun 22, 2019 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. Commented Jun 22, 2019 at 22:42

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.