I accidentally ran less
with an empty filename, which I expected to fail, but to my surprise, it printed some environment variables:
$ less -XE ""
export LESSOPEN="| /usr/bin/lesspipe %s";
export LESSCLOSE="/usr/bin/lesspipe %s %s";
(-XE
basically makes it behave like cat
.)
cat
, on the other hand, fails:
$ cat ""
cat: '': No such file or directory
And if you leave out the filename entirely, less
does error:
$ less -XE
Missing filename ("less --help" for help)
Why does less
do this? I can only imagine it being problematic, because if there's some bug in your code that causes an empty filename, you'll get bogus output. (Like, say, less "$(which nonexistent-script)"
.)
While we're here, why does it print those environment variables? Are they from my environment? They're identical:
$ declare -p LESSOPEN LESSCLOSE
declare -x LESSOPEN="| /usr/bin/lesspipe %s"
declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"
I checked man less
but the filename parameter doesn't seem to be documented explicitly. Other mentions of it didn't seem to be related to this behaviour. I also checked the less
FAQ.