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)

2 Answers 2

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?

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).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.