Ergänzend vielleicht noch ein paar interessante Punkte.
Man kann das auch mit ps
nachweisen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | $ ps -o pid,ppid,cmd
PID PPID CMD
4619 28604 ps -o pid,ppid,cmd
28604 28224 bash
$ ps -o pid,ppid,cmd | cat
PID PPID CMD
4623 28604 ps -o pid,ppid,cmd
4624 28604 cat
28604 28224 bash
$ { ps -o pid,ppid,cmd; } | cat
PID PPID CMD
7207 28604 bash
7208 28604 cat
7209 7207 ps -o pid,ppid,cmd
28604 28224 bash
|
Wenn man Zeilen 3, 7 und 14 vergleicht, sieht man, dass die geschweiften Klammern eine Subshell erzeugen.
Es gibt aber auch Fälle, die nicht ganz einheitlich mit der genannten Regel sind. So wird eine Variable z.B. nicht in der aktuellen Shell gesetzt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | $ unset foo
$ echo "($foo)"
()
$ foo=bar | cat
$ echo "($foo)"
()
$ { foo=bar; } | cat
$ echo "($foo)"
()
$ export foo=bar | cat
$ echo "($foo)"
()
$ { foo=bar; echo "($foo)"; } | cat
(bar)
$ echo "($foo)"
()
|
Die Erwartung wäre gewesen, dass Zeile 4 die Variable in der aktuellen Shell setzt, so dass man in Zeile 6 "(bar)" lesen würde.
Auch der Wert von $$ verhält sich seltsam - aber das macht er auch schon ohne Pipe:
| $ echo $$
28604
$ ( echo $$ )
28604
$ ( foo=bar; echo $$ )
28604
$ ( export foo=bar; echo $$ )
28604
|
Die Variable $BASHPID verhält sich dann eher wie erwartet:
| $ echo $BASHPID
28604
$ ( echo $BASHPID )
8633
$ ( foo=bar; echo $BASHPID )
9811
$ ( export foo=bar; echo $BASHPID )
9812
|
Jugend forscht. ☺