0

How write in if [ -z $str1 && -z $str2 ], "&&" doesn't work, if write [ -z $str1 ] this is work. I want to once two string

#!/bin/bash

echo "Enter first string"
read str1

echo "Enter second string"
read str2

if [ -z $str1 && -z $str2 ];    
then    
    echo "firs and second string length is zero"
else    
    echo "firs and second string length is not zero"    
fi
3
  • @JacobVlijm ok, can you help me?
    – qwerty
    Oct 31, 2014 at 19:14
  • 1
    try -a instead of &&. see man test
    – ppetraki
    Oct 31, 2014 at 19:17
  • FYI: Next time, pure script coding question should be on Stack Overflow.
    – Thomas Ward
    Oct 31, 2014 at 19:27

2 Answers 2

4

I am not the bash specialist on AU, but I have the feeling you mean:

#!/bin/bash

echo "Enter first string"
read str1

echo "Enter second string"
read str2

if [ -z "$str1" ] && [ -z "$str2" ]; then    
    echo "first and second string length is zero"
else    
    echo "first and second string length is not zero"    
fi

So that the output is "first and second string length is zero" only if both lengths are zero, right?

1

You can use -a in place of &&. From man test

   EXPRESSION1 -a EXPRESSION2
          both EXPRESSION1 and EXPRESSION2 are true

The && form of logical AND operator is valid within bash's extended test construct [[ ... ]], but not inside tests of the form [ ... ].

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .