Getting yes to Say no Three Times
Ask yes to say no three times and see what a line of text, an exit status, and a lone colon can do in a Unix shell.
$ yes no | head -n 3
no
no
noWho counted to three? head did. Over on the left, yes has been writing no all along. The vertical bar connects the output of one command to the input of the next. Once head has its three lines, it exits, closing the read end of the pipe. The next time yes tries to write, it normally gets a signal called SIGPIPE, which ends its run. Three lines make it to the screen. Linux pipe manual
Run yes on its own and it writes y, one line after another. Some programs ask for confirmation at every step and read each reply from standard input. Pipe yes into one of those and you can leave the keyboard. It will keep typing y and pressing Enter, however many questions come along. It never has to read one. Give it an argument and you can change its answer, too. The uutils yes manual
Even a command this small has a version number. With uutils coreutils 0.10.0, yes --version prints yes (uutils coreutils) 0.10.0. The name in parentheses tells you which collection of utilities supplied the command. Follow that name to the source and you’ll find Rust. The uutils yes.rs
The original loop took two lines
Back in 1979, you could take in the working parts of Seventh Edition Unix’s yes at a glance. Unix V7 yes.c
for (;;)
printf("%s\n", argc>1? argv[1]: "y");The empty spaces in for (;;) leave the loop with no stopping condition. Round it goes. Each time, printf prints the first argument on a line of its own. With no argument, it prints a y and a newline. On that Unix, yes no thanks would say no forever. It never got around to thanks.
In 2015, GNU coreutils gave its own yes a buffer. Speed hardly matters when you’re answering a few prompts, but you might also want a few gigabytes of repeating text as test input. Profiling showed how much time the program spent measuring the same strings and copying the same bytes. Each line was identical. Why assemble it all over again? Pack a buffer with whole lines once, and the same block can go out as often as you like. GNU’s optimization commit
In 2021, Robert Henry left a comment on that commit. “You're kidding. I practically watched Bill Joy write the first version of yes. 3 lines, 5 minutes, problem solved.” Henry’s comment
The benchmark in the commit runs yes >/dev/null. At least we can throw the ys away faster now.
You can still see the idea in the block-writing loop in GNU’s yes.c. GNU yes.c
if (bufused > copysize)
repeat_pattern (buf, buf, copysize, bufused);
while (full_write (STDOUT_FILENO, buf, bufused) == bufused)
continue;There’s one line in buf to begin with. If there’s room for more, repeat_pattern fills the space with copies. Each time the while loop writes a whole block, it goes straight back and writes it again. At the other end of our pipe, head still leaves after the third no, however many more are ready to go.
The screen says false
Give Bash a true-or-false question.
if echo false; then
echo "I'm in"
fiFirst the screen says false. Then it says “I’m in.” How did we get in?
In Bash, if runs the command that follows it and checks its exit status. Zero means success, so execution continues into the then branch. The job we gave echo was to write five letters to the screen. It did, and returned zero. What those five letters spelled never entered into the decision. Bash manual on conditional constructs
Rub out echo, leaving if false; then. This time Bash runs a command named false. It prints nothing and returns 1. The door stays shut. Try true and the terminal is just as quiet, but the status is 0. That’s how while true works, too. Before each round the loop asks again, and each time it gets the go-ahead.
Type either name in Bash and you’ll normally get a shell builtin. GNU coreutils also supplies two standalone programs. Look in their respective source files and, apart from a line to include a header, there’s one line apiece. true-true.c · true-false.c
/* true-true.c */
enum true_modes true_mode = TRUE_TRUE;
/* true-false.c */
enum true_modes true_mode = TRUE_FALSE;Both programs reach the same line at the end of their shared true.c: return EXIT_STATUS;. To find out what each one takes home, turn back to the top of the file. GNU true.c
#define EXIT_STATUS (true_mode != TRUE_TRUE)Read != as “differs from.” For true, the two sides are equal. The comparison comes out false, which gives C the value 0, exactly what the shell expects for success. For false, the sides really do differ, so it gets 1.
Even a colon is a command
In Bash’s colon.def, true shares its job with a punctuation mark. Both true and : lead to the function below. Even the argument has an apt name, ignore. Bash colon.def
int
colon_builtin (ignore)
WORD_LIST *ignore;
{
return (0);
}There are times when a command that does nothing is just what you need. Suppose a script hasn’t set a port number yet. Try these two lines in Bash.
$ : "${port:=8080}"
$ echo "$port"
8080The first line passes in silence. The second turns up a port number. This part of the job happens before the colon gets a turn. Bash has to work out which argument to hand it. When port is unset or empty, ${port:=8080} assigns it 8080 and expands to that value. By the time the colon receives 8080, the assignment is done. The colon is free to ignore it. Bash manual on parameter expansion
What if you rub out the colon at the start of the line? Bash still expands "${port:=8080}" to 8080. This time the number is standing where the command goes. Off goes Bash to look for a command called 8080.
