Question
What are the semantics of :
in regards to its use in pipes?
The Bash documentation states:
: [arguments]
No effect; the command does nothing beyond expanding arguments and performing any specified redirections. The return status is zero.
However, it's not obvious what behavior I ought to expect from :
being used in a pipe. Does it simply pass through std{in,out,err}
?
In regards to the arguments :
can be a stand-in for any other command, it seems. But I need a clarification regarding the use in pipe; a clarification not merely based upon observation, but citing authoritative sources.
Background
I have a situation, where -- depending on the environment the script runs in --, I need to run the output through tr -d '\r'
to remove carriage return characters from the output.
Alas, currently this means an if/else/fi
block, along the lines of (inside a function):
if [[ "$OSTYPE" == "cygwin" ]]; then
my_commands | tr -d '\r' || return $?
else
my_commands || return $?
fi
Where the | tr -d '\r'
is the only difference between the if
and the else
branch. The contents of either branch are of course quite a bit more complex than shown here.
Now, I get it that the tr -d '\r'
is benign in cases where there is no carriage return. But it's an invocation and (on Windows) an overhead that could be avoided (the function is a "hot" code path). And so I thought of replacing tr
by the builtin shell function :
.
pipefail
option, the|| return "$?"
will only check the exit status oftr
(itself unlikely to fail).if
, which may reduce some duplication:command_a | command_b | if [[ ... ]]; then command_c | tr -d' '\r'; else command_c; fi || return $?