There is no concept of a boolean variable in the shell.
Shell variables could only be text (an string), and, in some cases, that text may be interpreted as an integer (1, 0xa, 010, etc. ).
Therefore, a flag=true implies no truthfulness or falseness to the shell at all.
String
What could be done is either a string comparison [ "$flag" == "true" ] or use the variable content in some command and check its consequences, like execute true (because there are both an executable called true and one called false) as a command and check if the exit code of that command is zero (successful).
$flag; if [ "$?" -eq 0 ]; then ... fi
Or shorter:
if "$flag"; then ... fi
If the content of a variable is used as a command, a ! could be used to negate the exit status of the command, if an space exists between both (! cmd), as in:
if ! "$flag"; then ... fi
The script should change to:
flag=false
while ! "$flag"
do
read x
if [ "$x" == "true" ]
then
flag=true
fi
echo "${x} : ${flag}"
done
Integer
Use numeric values and Arithmetic Expansions.
In this case, the exit code of $((0)) is 1 and the exit code of $((1)) is 0.
In bash, ksh and zsh the arithmetic could be carried out inside a ((..)) (note that the starting $ is missing).
flag=0; if ((flag)); then ... fi
A portable version of this code is more convoluted:
flag=0; if [ "$((flag))" -eq 0 ]; then ... fi # test for a number
flag=0; if [ "$((flag))" == 0 ]; then ... fi # test for the string "0"
In bash/ksh/zsh you could do:
flag=0
while ((!flag))
do
read x
[ "$x" == "true" ] && flag=1
echo "${x} : ${flag}"
done
Alternatively
You can "Invert a boolean variable" (provided it contains a numeric value) as:
((flag=!flag))
That will change the value of flag to either 0 or 1.
Note: Please check for errors in https://www.shellcheck.net/ before posting your code as a question, many times that is enough to find the problem.