compare two characters

how can I compare two characters in shell script?

similar to C++ code:

char ch='a';
if(ch=='a')
{
do_something;
}

ch="a"
if [ $ch == "a" ]; then
  do_something
fi
1 Like

i have something like this and i get the error: ./proiect: line 19: [: ==: unary operator expected

while read -n1 char; do
if [ $char == "a" ]
then
((i++))
fi

done < file

Use = instead of ==

if [ "$char" = a ]
1 Like

this works perfect,thanks

BASH has == , which can cause some bad habits when many other shells don't.

1 Like