23

Wondering what use the yes command might be, I stumbled upon this comment, and tried to execute

yes $(yes yes)

From what I understand, this should simply print out an infinite sequence of yes, but instead it outputs nothing and crashes my graphical terminal after a few seconds. (If I execute it on tty1, I see the login prompt after some time.)

What is happening here?

  • 2
    I'm not an expert, but it might be that the first invocation is trying to print infinity infinite times. (or the argument may simply be too long.) – strugee Nov 10 '13 at 12:27
  • 5
    @strugee The argument list is never populated, because yes never returns. – Chris Down Nov 10 '13 at 14:58
  • @strugee You can get "infinity infinite times" using xargs: yes yes | xargs -P 0 yes (or without -P 0 for single-process infinity) – Kyle Strand Jan 06 '16 at 22:40

2 Answers2

29

It should already be enough to run

 echo $(yes yes)

The $(...) runs the inner command until it is finished and captures all its output. - Now as yes runs a long time and generates a lot of output, bash will eventually run out of memory and crash.

michas
  • 21,510
9

To see the message for the error which closes your shell, you need to run a sub-shell.

$ sh
$ kill -9 $$
Killed
$

$ sh
$ yes $(yes yes)
sh: xrealloc: cannot allocate 18446744071562067968 bytes
$
sourcejedi
  • 50,249
  • 3
    16 million gigabytes, eh? Gordon Moore is lurking in a corner going, "Well... I mean, not quite yet..." – FeRD Apr 15 '19 at 21:36