Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do some troubleshooting on my bash script. Is there a short and simple command that generates both stdout and stderr so that I can use 2>&1 on it? Sorry if this looks trivial, but I just can't think of one at the moment.

share|improve this question
ls . *blah (assuming you have some visible files in the current directory and don't have one ending in blah)... – jasonwryan Aug 13 '14 at 2:55
@jasonwryan, great! That works. Would you mind putting it down as answer? – Question Overflow Aug 13 '14 at 3:03
up vote 4 down vote accepted

A simple approach would be to use ls to list actual and imaginary files:

ls . *.blah

This assumes that there are visible files in the working directory and that you don't have any files that end in .blah1

1. ...and if you do, we won't judge you.

share|improve this answer
That won't work in zsh, fish, csh, tcsh and bash -O failglob. (and ls . may return nothing on stdout if the current directory only has hidden files/dirs). Better ls / /x – Stéphane Chazelas Aug 13 '14 at 7:02

Just write a subshell which sends to stdout and stderr...

(echo STDOUT && echo STDERR >&2) 

For proof that it works:

(echo STDOUT && echo STDERR >&2) > STDOUT.txt 2> STDERR.txt

This will create files STDOUT.txt and STDERR.txt containing the words STDOUT and STDERR respectively.

share|improve this answer
ls / /x
df / /x
wc / /etc/passwd
od / /dev/null

To guaranteed stdout written before stderr:

(w;/) # Bourne/csh like shells only.
sh -c 'w;/'
'time' w
share|improve this answer

You can write a function to use later:

gen_stdout_stderr() {                                                           
    printf "%s\n" "STDERR" >&2                                                  
    printf "%s\n" "STDOUT"                                                      


$ gen_stdout_stderr 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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