Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

This question already has an answer here:

Everytime I run my script the following if statement gives me the error;

script.sh: [Error==Error]: not found

or

script.sh: [Error==-2]: not found

if ["$P1"=="$P2"];then
            echo $name
fi

I've tried other versions

    if ["$P1"=="$P2"]
            then
            echo $name
    fi

and

    if [[ "$P1" == "$P2" ]]
            then
            echo $name
fi

P1="Error"
P2="$(sed -n '1p' somefile.txt)"

somefile.txt might contain a number or a string

share|improve this question

marked as duplicate by Gilles Nov 1 '14 at 22:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Take a look at shellcheck.net –  Cyrus Nov 1 '14 at 9:51

2 Answers 2

up vote 4 down vote accepted

Spaces are significant. Use:

if [ "$P1" = "$P2" ]

What went wrong

When the shell sees ["$P1"=="$P2"], it interprets it as a single word and looks for a command that matches that word. Since no such command exists, you get the not found error message.

share|improve this answer
    
Did exact same thing and got an ` [:` unexpected operator error. –  pyler Nov 1 '14 at 3:15
    
Had to use = instead of ==. works now. Thanks –  pyler Nov 1 '14 at 3:17

Assignment(=) in bash scripts is also compare operator in bash.

See this example:

if [ "$a" = "$b" ]

enter image description here Note the whitespace framing the =. In this case we are comparing "$a" and "$b".

if [ "$a"="$b" ] is not equivalent to the above.

Testing with example:

kasiay@kasiyaPC~:$ a=2
kasiay@kasiyaPC~:$ b=3

Then we run if [ "$a" = "$b" ]; then echo "equal"; else echo "not equal"; fi, the result is "not equal" and it's true result.

But if we rub if [ "$a"="$b" ]; then echo "equal"; else echo "not equal"; fi, the result is "equal" and it's wrong result!!

Why in this case we are wrong result?

When we are using if [ "$a"="$b" ], it parsing as if [ A_TOKEN ], then in this case the if condition always return true result. for example:

if [ "$a"="$b" ]; then    echo "TRUE"; fi
#result is TRUE

if [ 2=3 ]; then    echo "TRUE"; fi
#result is TRUE    

if [ anything ]; then    echo "TRUE"; fi
#result is TRUE

Both = and == are string comparison.

The == comparison operator behaves differently within a double-brackets test than within single brackets.

[[ $a == z* ]]   # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

[ $a == z* ]     # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).

links:Comparison Operators

Where you are using wrong statement?

In if ["$P1"=="$P2"];then, should be if [ "$P1" == "$P2" ];then. Spaces around == and also after and before brackets.

share|improve this answer
    
= is not a bash operator here; it is an argument to [. –  chepner Nov 1 '14 at 13:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.