-4

What does -z "${line}" mean in the if condition of the following code:

while read line
do
if [ -z "${line}" ]; then
echo "file empty "
exit
fi
done < $FILE

Please let me know what exactly what the if condition is checking.

1

1 Answer 1

3

It is testing if the variable $line (equivalent here to ${line}) is empty (containing no chararacters). You would get that case when the read operation reaches the end of the file.

A followup comment asked what the z is for (what it means). The [ operator corresponds to the test utility, with POSIX stating

-z string
True if the length of string string is zero; otherwise, false.

so the -z was probably chosen originally for its mnemonic value. It was not provided in the oldest version of test (Unix 7th, 1979), but appeared in the 8th edition (1985):

-z s1    true if the length of string s1 is zero.

Because it was undocumented in the 7th edition, but appeared around the same time in 3BSD (file dated February 1979 in CSRG CD1), it appears to have from from BSD.

Not all of the options in test have an obvious relationship between the letter and meaning. This one came fairly early, so it was possible to choose a letter which can be related to the meaning.

2
  • and what is the -z for ?? Commented Sep 13, 2016 at 9:43
  • like zero ? : the length of STRING is zero
    – Archemar
    Commented Sep 13, 2016 at 10:21

You must log in to answer this question.

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