I unerstand this part: command -v kubectl > /dev/null 2>&1
so if it fails or not we redirect the output to /dev/null in which if I understand it correctly means ignore it in all cases.
You've misunderstood that; no part of command -v kubectl > /dev/null 2>&1
has any connection to "if it fails" vs. "not".
Rather, >
and 2>
redirect two different types of output; >
(or 1>
) redirects whatever the command writes to standard output, and 2>
redirects whatever the command writes to standard error. Whether the command writes anything to standard output is independent of whether it ends up succeeding or failing; whether it writes anything to standard error is independent of whether it ends up succeeding or failing; and whether it writes anything to standard output is independent of whether it writes anything to standard error.
For example, here is a command that prints to both standard output and standard error, and then returns successfully:
( echo "this is on standard output" ; echo "this is on standard error" >&2 ; exit 0 )
and here is one that prints to both standard output and standard error, and then fails:
( echo "this is on standard output" ; echo "this is on standard error" >&2 ; exit 1 )
By contrast, ||
really does have to do with whether a command succeeds (exiting with zero) or fails (exiting with something other than zero).