Quote the variable:
if [ ! -z "$line" ]
but normally, one would write:
if [ -n "$line" ]
When you leave the variable unquoted, the [
command sees something like: [ -n cat dog ]
, which is an error because it expects only one argument after -n
. By quoting the variable, the expression becomes [ -n "cat dog" ]
which has only one argument, as expected by [
. Note that there's really no reason to do that test, or to use set
; read can split the line for you when it reads:
while read animal sound offspring; do
test "$animal" = "$token" && echo $animal $sound $offspring
done < $file