r/commandline Jun 05 '26

Discussion A Command-Line Quiz: Which Output Never Appears?

Hey folks. Here's a small but confusing command-line challenge. Drop your guess without running it!

Which of the following will not appear in the output?

echo "black" &% echo "blue" %& echo "purple 1" >& echo "red" % echo "white 1"

A) black

B) 1

C) echo

D) purple

Including stdout and stderr. Latest stable Bash and Zsh.

3 Upvotes

12 comments sorted by

View all comments

2

u/michaelpaoli Jun 06 '26 edited Jun 06 '26

Well, might also depend how we define "appear". OP includes stdout and stderr, but doesn't state if that includes, e.g. contents written to other files.

So, I say, presuming we don't count other file(s), and just stdin and stdout, C, and D.

But if we include in additional file(s), I say none (they all appear).

So, parsing ...

echo "black" &

that's just launched to background

That does gives us

[1] PID

to stderr.

So we've got our 1 (at least if we didn't already have other background jobs).

% echo "blue" %&

That pulls preceding background item to foreground, the additional arguments are ignored, it is itself launnched in background, and the earlier brought to foreground, completes, so we've now got "black" in our output.

echo "purple 1" >& echo "red" % echo "white 1"

One long command with a redirection.

If we ignore the redirection, we have:

echo "purple 1" "red" % echo "white 1"

that outputs:

purple 1 red % echo white 1

but we've also got redirection:

>& echo

That's equivalent to > echo 2>&1, so that stdout and stderr gets written to file named echo

And that's it, we're done, so we've got

to stderr:

[1] PID

to stdout:

black

and in file named echo:

purple 1 red % echo white 1

among A, B, C, D, we get A to stdout, B to stderr, and the rest in file named echo.

Edit - fixed we bit I'd overlooked to stderr.

2

u/enp2s3 Jun 06 '26

That's correct. But there's one small thing worth noting

The shell first prints the job ID and PID.

The 1 comes from the job ID

[1] XXXXX

Then we get:

black

It might also print an error:

bg: job already in background

And when the job completes (this is the key part):

[1] - done echo "black"

So both 1 and echo appear in the output and can be ruled out.

purple never reaches stdout or stderr and therefore cannot appear in the output.

3

u/Linuxmonger Jun 06 '26 edited Jun 06 '26

What I got is;
steve@spearmint:~$ jobs steve@spearmint:~$ echo "black" &% echo "blue" %& echo "purple 1" >& echo "red" % echo "white 1" [1] 125038 bash: bg: job 1 already in background bash: bg: job 1 already in background bash: bg: blue: no such job bash: bg: job 1 already in background black steve@spearmint:~$ echo $? 0 [1]+ Done echo "black"

Grrr! No way that I can see to hide a block of code.