0

Is a command somehow saved/possible to be referenced to in a pipe? For example:

cd /tmp/non_existing_dir || echo "could not execute $PREVIOUS_PIPE_CMD

without pre-saving the original command. $PREVIOUS_PIPE_CMD is an example variable.

Zloj
  • 2,235
  • 2
  • 18
  • 28

1 Answers1

1

No. Piping works by black-boxing and relaying IO streams and statuses. There is no way for the command that has been piped into to know the downstream history without relaying it/saving it and passing it around.

Also: your example is not using piping, it is using ||, which is OR, not a pipe.

cd /tmp/non_existing_dir || echo "could not execute $PREVIOUS_PIPE_CMD"

is the same as

if [[ $(cd foo) ]]; then
  :
else
  echo "could not execute $PREVIOUS_PIPE_CMD
fi

Given your example you would want:

tmpdir="/tmp/non_existing_dir"
cd ${tmpdir} 2>/dev/null || echo "could not cd into '${tmpdir}'"
Marc Young
  • 3,854
  • 3
  • 18
  • 22