nerdexam
LPI

101-500 · Question #507

Which of the following commands redirects the output of ls to standard error?

The correct answer is C. ls >&2. You've hit your Sonnet limit · resets Jun 8, 9am (UTC)

Shells and Shell Scripting

Question

Which of the following commands redirects the output of ls to standard error?

Options

  • Als >-1
  • Bls <<ERR
  • Cls >&2
  • Dls >>2
  • Els |error

How the community answered

(23 responses)
  • A
    9% (2)
  • C
    87% (20)
  • E
    4% (1)

Explanation

You've hit your Sonnet limit · resets Jun 8, 9am (UTC)

Topics

#I/O redirection#file descriptors#stderr#shell syntax

Community Discussion

9
Lena V.Lena V.Jan 19, 2026

C is the right answer. In bash, file descriptor 1 is stdout and file descriptor 2 is stderr, and the >&2 syntax redirects the preceding command's stdout to whatever file descriptor 2 points to, which is stderr. The other options are all wrong for clear reasons: >-1 is not valid syntax, <<ERR opens a here-document expecting input, >>2 would append stdout to a literal file named "2", and the pipe to "error" just tries to run a nonexistent command. This is a core redirection mechanic that shows up constantly on the 101-500 so burn it into memory now.

17
Nina C.Nina C.Jan 21, 2026

Good breakdown, and worth tacking on that the order matters when you chain these, so something like 2>&1 1>/dev/null does not do what most beginners expect because stderr gets bound to stdout's original destination before stdout is redirected.

0
Bao N.Bao N.Feb 6, 2026

C is your answer. The >&2 syntax redirects stdout to file descriptor 2, which is standard error, and that is the only option here that actually uses valid shell redirection to send output to stderr.

10
Nina C.Nina C.Feb 2, 2026

Got this one on my practice test last week and froze, but then I remembered that 2 is always stderr, so >&2 sends output there. C is it.

3
Grace U.Grace U.Feb 4, 2026

C is the right call here. The >&2 syntax tells the shell to send file descriptor 1 (standard output) to wherever file descriptor 2 (standard error) is pointing, which is exactly how you redirect output to stderr in bash.

3
Orla P.Orla P.Feb 20, 2026

Honest confession, I circled D first because I saw the 2 and thought yeah, stderr is file descriptor 2, so appending to 2 must do it, but that just writes to a file literally named 2 in your current directory. The redirect operator needs the ampersand to tell the shell you mean a file descriptor and not a filename, so ls >&2 is the one that actually sends standard output over to standard error. The other distractors are easier to rule out once you know that, since B is a here-doc for input, not output, and E is just piping to a command called error that does not exist. Lock in the pattern fd>&fd and you will not second-guess this one on test day.

3
Marit C.Marit C.Mar 5, 2026

C is right, >&2 duplicates stdout onto file descriptor 2.

1
Luis F.Luis F.Mar 8, 2026

C is the one, ls >&2 redirects stdout to file descriptor 2 which is stderr. Option D trips people up because ls >>2 just appends output to a literal file named "2", not to the error stream.

1
Ola B.Ola B.Feb 26, 2026

The answer is C, and if you spin up any Linux box right now you can confirm it in about ten seconds. The redirect operator >&2 sends file descriptor 1 (stdout) into file descriptor 2 (stderr), which is exactly what the question is asking. Option D trips up a lot of people because ls >>2 looks like it has something to do with file descriptor 2, but without the ampersand the shell treats that 2 as a literal filename and appends stdout to a file called 2 sitting in your current directory, which is not stderr at all. I do think the question could be worded more precisely since technically you are redirecting stdout to the same destination as stderr rather than redirecting "to standard error" in some absolute sense, but on the exam and in practice >&2 is the correct and standard way to get that behavior.

0
Full 101-500 Practice