Paul Evans's answer has the proper solution: you need (at least) a space character around operators such as = (and its equivalent in bash, ==).
As for why you needed that space:
Bash allows string concatenation simply by placing tokens next to each other without whitespace in between.
Whether the tokens are quoted or not, and what quotes are used is irrelevant; e.g.:
'ab'cd"ef" # -> Bash sees: abcdef
In the case at hand:
"$flag1"="C" # Bash eventually sees: <value-of-$flag1>=C
In other words: "$flag1"="C" evaluates to a single, nonempty string literal, and applying [ ... ] to such a string always evaluates to true.
Generally, see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128 in the POSIX shell spec. for information on how [ and its alias test treat differing numbers of arguments (thanks, @glennjackman).
For Bash to recognize the intended syntactic elements of "$flag1"="C" as such, you must therefore separate the elements with at least one space each.: "$flag1" = "C"
If you want your solution to be more bash-like, however, consider use of [[ ... ]] rather than POSIX syntax [ ... ]:
if [[ $flag1 == "C" && $flag2 == "C" ]] # ...
Benefits:
- Allows you to use
&& inside a single [[ ... ]] construct.
- Obviates the need to double-quote your variable references (though double-quoting never hurts and is actually important on the right side of
= / == to distinguish string literals from patterns).
- Performs slightly better.
Compatibility note: While [[ ... ]] works in bash and also zsh and ksh (though the behavior differs subtly), it is NOT POSIX-compliant. In situations where that is a concern, stick with [ ... ].
=signs.sh testif.shwill override the shebang, forcing this to be interpreted asshinstead ofbash?#!/bin/bashdefines the default interpreter for the script in question -- the interpreter used if no interpreter is called explicitly (e.g. when you'd be calling./testif.shdirectly). When you're callingsh testif.sh, you are specifyingshto interpret the script. Now, most systems make/usr/bin/sha link to/bin/bash-- but if invoked this way, bash will run in compatibility mode, mimicking originalshbehaviour. Stuff like this can result in surprising errors -- for example, redirecting script output viaexec > >(tee logfile.txt)will not work inshmode.