0

Sometimes shell commands behave unexpectedly because of subshells-processes.

A recent example I experienced was that this command won't work because xargs runs the cd-command in a subshell:

ls | sort | tail -1 | xargs cd

Another one is that cd doesn't work in Makefiles.

Is there a way to visualize which shells/subshells/processes do what in a (chained) command? I would like to get a better feeling for them.

Kaligule
  • 630
  • 3
  • 15

1 Answers1

0

Maybe the following utility could help to debug pipe statements:

function _() {
  local code=0
  "$@" || code=$? > >(tee -a "$1".out) 2> >(tee -a "$1".err >&2)
  echo Exited with "$code" >> "$1".out
}

Usage:

_ ls | _ sort | _ tail -1 | xargs _ cd