0

We're doing workshops for internships where we explain the Bash. One topic is redirection, because it's quite important.

So far we explained > and 2> independent from each other. Our examples are

echo Hello world
echo Hello world > file.txt
let x=1/0
let x=1/0 2> file2.txt

For programmers with an understanding of the concepts of stdout and strerr, that's not a problem.

However, for interns, the only effect in both cases is: the output is gone and was redirected into a file.

What I'm looking for is a command that produces both, regular output (stdout) and error output (stderr). That way we can use > to redirect only a part of the output, 2> to redirect the other part or both to get 2 files.

The command should be rather simple and not involve too much preparation. The more preparation, the more likely that they will get something wrong.

3
  • Considering the file handles 0, 1, 2 are references to stdin, stdout, stderr, wouldn't it be better just to explain those to them? A short walk through the basic concepts, "everything is a file", "to communicate with a file, you need a file handle", "the first three file handles are special" etc. shouldn't take that long and would, in my estimation, be more effective than just teaching your interns cryptic numbers without them grokking the background. – Henning Kockerbeck Feb 20 '20 at 12:33
  • 1
    @HenningKockerbeck: We already and still explain it, but we get questions like "why can't we create a directory with touch if everything is a file?", "why do I need rmdir instead of rm to remove a directory, if a directory is a file as well?", "Why can't I use chmod +d to make a file a directory?" Actually we're discussing about the removal of that explanation. – Thomas Weller Feb 20 '20 at 12:51
  • I wouldn't avoid those questions, I'd welcome and forster them. Questions like this are signs that the concepts are starting to sink in. Just answer them :) – Henning Kockerbeck Feb 20 '20 at 12:56
2

I'd use something like

ls file not-a-file

where file exists in the current directory and not-a-file doesn't. Obviously you could substitute ls for any other simple command that takes filename arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.