calling-commands
Checking Return Values
Always check return values and give informative return values.
For unpiped commands, use $?
or check directly via an
if
statement to keep it simple.
Example:
Bash also has the PIPESTATUS
variable that allows
checking of the return code from all parts of a pipe. If it's only
necessary to check success or failure of the whole pipe, then the
following is acceptable:
However, as PIPESTATUS
will be overwritten as soon as you
do any other command, if you need to act differently on errors based
on where it happened in the pipe, you'll need to assign
PIPESTATUS
to another variable immediately after running
the command (don't forget that [
is a command and will
wipe out PIPESTATUS
).
Builtin Commands vs. External Commands
Given the choice between invoking a shell builtin and invoking a separate process, choose the builtin.
We prefer the use of builtins such as the Parameter Expansion
functions in bash(1)
as it's more robust and portable
(especially when compared to things like sed
).
Examples: