#!/usr/bin/env bash
sleep 3 & # Spawn a child
trap '
pgrep -P $$ # Outputs one PID as expected
PIDS=( $( pgrep -P $$ ) ) # Saves an extra nonexistant PID
echo "PIDS: ${PIDS[@]}" # You can see it is the last one
ps -o pid= "${PIDS[@]:(-1)}" ||
echo "Dafuq is ${PIDS[@]:(-1)}?" # Yep, it does not exist!
' 0 1 2 3 15
出力します
11800
PIDS: 11800 11802
Dafuq is 11802?
トラップでのみ発生します。存在しない PID が配列に追加されるのはなぜですか? そして、この奇妙な動作を回避するにはどうすればよいですか?