Questions about file descriptors

Hi, I'm playing with KSH

I entered following command in terminal

{ echo "stdout" >&1; echo "stderr" >&2; } > out

And I get only stoud in a new file out.

My question is: Where did my stderr vanish ?

Hi,

Have a look at "syslog.conf" file and check where it goes.

Traditiionally "stderr" defaults to the user output (screen) - it could have changed in Solaris 11.2 - but I'd be really surprised. You are looking for lines in the syslog.conf that will say something like.

user.err                                        /dev/sysmsg
user.err                                        /var/adm/messages
user.alert                                      `root, operator'
user.emerg                                      *

Regards

Dave

1 Like

Hi gull04,
Settings in syslog.conf affect where messages printed by syslog go; not where messages printed by echo go.

Hi solaris_user,
From what you have shown us, the word stdout should have appeared in the file named out and the word stderr should have appeared on your terminal's screen unless you had an earlier exec statement in your shell redirecting file descriptor 2 to another file or you redirected the file descriptor 2 output of the shell script containing the statements you showed us to another file.

If you wanted both stdout and stderr from your subshell to be saved in the file named out you would need soething like:

{ echo "stdout" >&1; echo "stderr" >&2; } 2>&1 > out

I assume that you already know that >&1 is shorthand notation for 1>&1 which redirects the output directed to file descriptor 1 to be written to file descriptor 1 (which is a no-op), so the code shown in red above can be deleted without affecting the results.

1 Like

Hi Don, thanks for clear explanation. I didn't know for that shorthand.
So when I do something like

ls -al > mylist

Shell that interprets like

ls -al 1> mylist

Is this correct ?

Yes. Look at the ksh man page for redirections.

When you use set -x to trace command execution with ksh , it also shows redirections, for example:

ls -al > list
+ ls -al
+ 1> list

Unfortunately, bash doesn't seem to trace redirections (even when the redirection includes variable expansions).