Reputation: 348
if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi
The test-commands list is executed, and if its return status is zero, the consequent-commands list is executed.
Show status of ''=1
in bash.
''=1
bash: =1: command not found
echo $?
127
The value of status is 127, not zero. A strange statement:
if [ ''=1 ] ; then echo "true"; else echo "false"; fi
true
Why value of status 127
, not zero invoke then statement? Why can't get false
in bash?
@Дмитрий Шатов
=1
bash: =1: command not found
echo $?
127
Upvotes: 0
Views: 365
Reputation: 73
This is because it is a string comparison operation.
if [ "$a" = "$b" ]
then
echo 'true'
else
echo 'false'
fi
You need to use !=
rather than =
when comparing the two strings. This will act as the opposite of the comparison [ "$a" = "$b" ]
if [ "$a" != "$b" ]
then
echo 'false'
else
echo 'true'
fi
Upvotes: 1
Reputation: 8972
''=1
gets interpreted as a string =1
and test finds a non-empty string. If you want to compare ''
to 1
, write
[ '' = 1 ]
man bash
... If that function is not defined, the shell prints an error message and returns an exit status of 127.
Upvotes: 3