last day (16 days later) » 

13:44
6
Q: Why does -z and -n exist in most shells and /bin/test?

Evan CarrollThe test command on Unix-like systems provides two special syntax forms for checking whether a string is empty or not: test -z "$foo" # the length of $foo is zero test -n "$foo" # the length of $foo is nonzero These forms are actually redundant, and equivalent to the following: test "$foo" = "" ...

Is this a question about bash? Or some other shell?
@OmarL I assume it started with test which seems to be now-standardized and how shell scripts executed expressions. Certainly predates bash. pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html Not sure what started the convention and what the reason was. That's why I'm here.
Mind to add what OS, what environment and so on this is about?
@Raffzahn I'm asking a question about the test. I'm not even sure how to answer that. I have no idea what operating system or Unix first implemented it with a -z and -n option.
@EvanCarroll Well, then maybe start with in which environment you have seen (or still do) this behaviour, and what 'test' is supposed to mean in this context. is it an OS command, a program or alike (since you use quotation used for that) or is it a command of some application? Also, there is not much good in answering me (or others) in comments - better try to improve the question in a way that anyone reading get an idea what it's about.
13:44
yes, test is an actual utility /bin/test that was afaik used in the first shell scripting languages. It provides an ability to evaluate expressions using a program: /bin/test "$foo" -eq 5 would tell you if $foo was equal to 5, the equivalent of "$foo" == 5 today. This interface was present in Almquist shell (/bin/sh), and passed down to bash /bin/bash. It provides both a -z and -n option in addition to simply doing = "" and != "". I'm not sure how much of my history (which may be wrong) is necessary to ask a question and seek out experts.
@EvanCarroll Wouldn't it be great if a question is not only readable to super experts who are in exactly the same mind set as you, but even lesser beings? This might especially come helpful as what you ask - and it's answer - is preserved to help future seekers of knowledge. Putting more information into the question (instead of comments who may get deleted) helps everyone, even the experts to understand were the question is rooted.
Looking at the link that is given in one of the comments, I see:"The two commands: test "$1" test ! "$1" could not be used reliably on some historical systems. Unexpected results would occur if such a string expression were used and $1 expanded to '!', '(', or a known unary primary. Better constructs are: test -n "$1" test -z "$1" respectively." So the -z and -n flags was there from the beginning and are kept to insure comparability.
@Raffzahn fair enough I tried to make it cleaner.
@Raffzahn, it was quite clear from the start what the question is about. Just because you haven’t ever heard of Unix doesn’t mean nobody else did.
@user3840170 RC.SE should be welcoming to readers from all backgrounds, don't you think so? Also, please don't fall back into your old habit.
13:44
@EvanCarroll: bash doesn't descend from the Almquist shell. In fact, according to Wikipedia, the Almquist shell was only released a week before bash.
@user3840170 be careful when you change syntax those "`" were intentional there. The back ticks tell the shell to launch a sub shell and replace the expression with the return code.
@ninjalj that's why I didn't want to do the history lesson there, and I just wanted to ask the question. Thanks! Feel free to update with the background needed for the question
@Raffzahn I don't think I need to provide a history lesson to ask a question about history. ;) As you can see here I probably was wrong on some of it. My question is on shell, and it should make sense to the people who know about it. It's a high barrier to pull everyone along for it. Look at your own question here, retrocomputing.stackexchange.com/q/14027/8579 I have no idea what HBL and VBL are or why it would not be possible to detect it. It's only tagged apple-ii, but it's about unexplained display technology. that's fine to me.
@EvanCarroll: No, they really were redundant. The backticks don’t interpolate as the return code, but the contents printed to standard output; the if command collects the return code from $?. Though luckily enough, test prints nothing, so backticks still work. (By the way, test has a synonym [ that exists purely for aesthetic reasons, so that one can write conditions that resemble other programming languages somewhat. It would have been strange to have to wrap [ in backticks as well.)
@EvanCarroll Well, you got me ... at least in part, as HBL is explained - just read the title line :)) Then again, Apple II is a rather well defined environment, unlike 'a' shell. Also, the question was more of an information dump, as I did answer it right away. It might help to read it with its answer. It was made to collect information as the topic did come up in a different question. But yes, you're right, it won't hurt if I do add a bit more.
Which one do you think should stay, and which one should go?
@ThorbjørnRavnAndersen my preference would be == "" and != "". I can't see anyone arguing -z and -n are more descriptive of the condition you're testing.
13:44
@evan but which one should go?

  last day (16 days later) »