0

openSUSE Tumbleweed has

test -s ~/.alias && . ~/.alias || true

as the contents of its ~/.bashrc.

The way I understand the part to the left of || true is that

  • there's a check to see if ~/.alias exists and has a size greater than zero.
  • and, if the conditions are met, the file is sourced.

So why is || true necessary?


GNU bash, version 5.0.16(1)-release (x86_64-suse-linux-gnu)

DK Bose
  • 947

2 Answers2

1

The || true ensures that the whole command returns a "Success" status, even if ./.aliases returns non-zero (Failure) status.

Handy if you're using set -e (exit on error).

waltinator
  • 4,865
1

|| true is useful in contexts where we don’t care if the command fails; in this particular case, if ~/.alias doesn’t exist, test -s will fail with a non-zero exit code, but we don’t want that to have any other consequence. || true ensures that the full command list (including sourcing ~/.alias) always exits with a successful status.

This would be particularly relevant if the .bashrc included set -e, although that would be unusual for a shell startup script.

Another way to write this would be

if test -s ~/.alias; then . ~/.alias; fi

but that would exit with a non-zero exit code if sourcing ~/.alias caused an error.

See also Why is pattern "command || true" useful?

Stephen Kitt
  • 434,908