[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: test -f with no args is true
From: |
Bob Proulx |
Subject: |
Re: test -f with no args is true |
Date: |
Sat, 15 Apr 2006 12:52:07 -0600 |
User-agent: |
Mutt/1.5.9i |
Charlie Bennett wrote:
> Is this expected behavior?
>
> [root@build-04 .ssh]# [ -f ]
> [root@build-04 .ssh]# echo $?
> 0
Yes. That is expected behavior. With one argument the return code is
true if the string is non-zero in length. The case covered is this
case:
if [ "$variable" ]; then
echo variable contains data
fi
Because you can enter any data you want into variable if it were
interpreted as a -f operator then you would get the dreaded "test:
argument expected" message as was often seen before this rule came
into place. POSIX requires this behavior. See this document:
http://www.opengroup.org/onlinepubs/009695399/utilities/test.html
The pertinent lines are:
0 arguments:
Exit false (1).
1 argument:
Exit true (0) if $1 is not null; otherwise, exit false.
2 arguments: ...
Bob
Re: test -f with no args is true, Paul Jarc, 2006/04/15