the special "test" expression `-l STRING'

I have searched and searched in regards to this documented 'special expression ‘-l STRING’ for the ‘test’ command, and to no avail, have I found out why it does not work on my system.

The example always given is:

test -l abc -gt 1 && echo yes

And the returned result is:

bash: test: -l: unary operator expected

The documention is usually as follows:
Numeric tests
Numeric relationals. The arguments must be entirely numeric (possibly negative), or the special expression `-l STRING’, which evaluates to the length of STRING.

Then examples are given, including the one I provided above.

Does anyone else have this issue of getting an error when trying this special expression of ‘l STRING’ ???


yes and a different distro, but I note from the man page

NOTE: your shell may have its own version of test and/or , which usually supersedes the version described here. Please refer to your shell’s documentation for details about the options it supports.

And bash test doesn’t look like it has it test MAN Page Edit: mmm missed it now I wonder why, well I worked around it does that count :wink:

I maybe of the mark but I think this is covered by a builtin

 ${#a} -lt ${#b} ]; echo $?
 ${#a} -gt ${#b} ]; echo $?

Far from a bash guru but that was the conclusion I came to. The bash test is different and has the ability to reference length by ${#string}. Though I may well stand corrected by the ones who know more.

man 1p test says:

The following option was not included because it was undocumented in
most implementations, has been removed from some implementations
(including System V), and the functionality is provided by the shell
(see Parameter Expansion .

   -l  string
          The length of the string string.

So it’s not even included in POSIX. I would say that whatever doco you are reading describes a particular implementation.

Thanks for the input folks - it has beed educational.

So, apparently, there is a shell builtin version of ‘test’, and then there an external executable version of ‘test’, and the behavior of them varies somewhat.

~ $ whereis test
test: /usr/bin/test /usr/bin/X11/test /usr/share/man/man1/test.1.gz

~ $ /usr/bin/test -l abc -gt 1 && echo yes

~ $ /usr/bin/X11/test -l abc -gt 1 && echo yes

Same thing for “” as well:

~ $ whereis 
: /usr/bin/ /usr/bin/X11/

~ $ /usr/bin/ -l abc -gt 1 ] && echo yes

Fascinating. :slight_smile:


should be the same thing

ls -ll $(whereis )
ls: cannot access :: No such file or directory
lrwxrwxrwx 1 root root 4 Mar 18 22:49 /usr/bin/ -> test

Ken is on the money afaik the behaviour is depreciated a quick test shows it belongs to coreutils.

man -a test quit the first one and you should get test (1p), the other thing is looking in man bash there is no mention of it. It would seem to be only this man page, so I suspect outdated doc.