Consider the following script..
str1="Good"
str2="Bad"
str3=
[ $str1 = $str2 ]
echo $?
[ $str1 != $str2 ]
echo $?
[ -n $str1 ]
echo $?
[ -z "$str3" ]
echo $?
[ -z $str3 ]
echo $?
[ "$str3" ]
echo $?
When I executed this, the o/p is
But in my book, the op is totally different,
What should I do ?? Which one I should accept ??
If you understand what is being tested, you'd know which one to accept. Plus, you would accept the one you actually see executed, right?
[ $str1 = $str2 ]
tests to see if $str1 is equal to $str2. They are not so return false (1).
[ $str1 != $str2 ]
tests to see if $str1 is not equal to $str2. They are not so return true (0).
[ -n $str1 ]
tests to see if $str1 is not empty/null. It is not so return true (0).
[ -z "$str3" ]
[ -z $str3 ]
These are identical and test to see if $str3 is null. It is so return true (0).
[ "$str3" ]
tests to see if $str3 in not empty (-n is implied). It is so return false (1).