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.
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.
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}'"