Skip to main content
Fixed sheband, add missing quotes.
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k

Yes, if you care about the exit code of the compound statement. Try the following:

#! /bin/sh -

! $1"$@" || true
echo $"$?"

$1"$@" && true
echo $"$?"

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check What does set -e mean in a bash script? to see possible implications of set -e, trap and set -o pipefail.

Yes, if you care about the exit code of the compound statement. Try the following:

#/bin/sh

! $1 || true
echo $?

$1 && true
echo $?

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check What does set -e mean in a bash script? to see possible implications of set -e, trap and set -o pipefail.

Yes, if you care about the exit code of the compound statement. Try the following:

#! /bin/sh -

! "$@" || true
echo "$?"

"$@" && true
echo "$?"

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check What does set -e mean in a bash script? to see possible implications of set -e, trap and set -o pipefail.

https link, clarify granmmar
Source Link
mmoya
  • 6.3k
  • 2
  • 23
  • 23

Yes, if you care about the exit code of the compound statementexit code of the compound statement. Try the following:

#/bin/sh

! $1 || true
echo $?

$1 && true
echo $?

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check this SO answerWhat does set -e mean in a bash script? to see possible implications of set -e, trap and set -o pipefail.

Yes, if you care about the exit code of the compound statement. Try the following:

#/bin/sh

! $1 || true
echo $?

$1 && true
echo $?

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check this SO answer to see possible implications of set -e, trap and set -o pipefail.

Yes, if you care about the exit code of the compound statement. Try the following:

#/bin/sh

! $1 || true
echo $?

$1 && true
echo $?

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check What does set -e mean in a bash script? to see possible implications of set -e, trap and set -o pipefail.

Add link to SO question about set -e
Source Link
mmoya
  • 6.3k
  • 2
  • 23
  • 23

Yes, if you care about the exit code of the compound statement. Try the following:

#/bin/sh

! $1 || true
echo $?

$1 && true
echo $?

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check this SO answer to see possible implications of set -e, trap and set -o pipefail.

Yes, if you care about the exit code of the compound statement. Try the following:

#/bin/sh

! $1 || true
echo $?

$1 && true
echo $?

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Yes, if you care about the exit code of the compound statement. Try the following:

#/bin/sh

! $1 || true
echo $?

$1 && true
echo $?

then run it with true and false as arguments.

./script true
0
0

./script false
0
1

This is because short-circuit evaluation of boolean expressions.

In OP's example, let's suppose the file doesn't exist:

  • In the first case, the first condition returns TRUE, no need to evaluate the second operation (TRUE OR x = TRUE). You get a TRUE for the compound statement.
  • In the second case, the first condition returns FALSE, no need to evaluate the second operation (FALSE AND x = FALSE). You get a FALSE for the compound statement.

Exit codes are very important. Check this SO answer to see possible implications of set -e, trap and set -o pipefail.

Source Link
mmoya
  • 6.3k
  • 2
  • 23
  • 23
Loading