> Hongyi Zhao <
hongy...@gmail.com> writes:
> > 在 2020年4月12日星期日 UTC+8下午2:47:45,Hongyi Zhao写道:
> >> See the following test:
> >>
> >> $ ps -o pid,ppid,pgid,gid,sess --ppid $$ | awk '1'
> >
> > My typo, I mean the following:
> >
> > $ ps -o pid,ppid,pgid,gid,sess --ppid $$ 2>&1 | awk '1'
> > PID PPID PGID GID SESS
> > 24092 21819 24092 1000 21819
> > 24093 21819 24092 1000 21819
> >>
> >> I've already redirected both stdout and stderr to the pipe, why the
> >> results will be printed twice?
>
> It's not printing the same thing twice. Those are two different PIDs.
>
> Adding ",cmd" shows what's going on:
>
> $ ps -o pid,ppid,pgid,gid,sess,cmd --ppid $$ 2>&1 | awk '1'
> PID PPID PGID GID SESS CMD
> 18708 10491 18708 1000 10491 ps -o pid,ppid,pgid,gid,sess,cmd --ppid 10491
> 18709 10491 18708 1000 10491 awk 1
> $
>
> It's printing lines for the ps process and the awk process, both of
> which share the same PPID (namely $$, your current shell process).
>
> In general, when you're trying to solve something like this, think about
> adding more information to the output. The raw numeric values were not
> terribly useful.