Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Understanding Bash short-circuiting

First of all I'm not a Bash pro. I discovered few months ago that if I use both the && and || short circuit operators in sequence with curly braces, then in case the first statement exits with a truthful value, if the last statement in the true block exits non-zero, then the fail block will be executed too. Like this:

returnNumber 0 && {
    echo 'OK'
    returnNumber 1
} || {
    echo 'NG'
}

Will output:

OK
NG

So, I looked for the easiest solution for this, and came up with this:

returnNumber 0 && {
    echo 'OK'
    returnNumber 1
    :
} || {
    echo 'NG'
}

I know, it is easy to leave out the colon builtin, but is it a proper way for a workaround?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This is actually a very common Bash pitfall. It is not a bug.

returnNumber 0 evaluates to true, so the second block (joined by logical and &&) is evaluated as well to make sure the result of first && second is still true.
The second block outputs OK but evaluates to false, so now the result of first && second is false. This means that the third portion (joined by logical or ||) must be evaluated as well, causing NG to be displayed as well.


Instead of relying on && and ||, you should be using if statements:

if returnNumber 0; then
    echo 'OK'
    returnNumber 1
else
    echo 'NG'
fi

tl;dr: Never use x && y || z when y can return a non-zero exit status.

over 4 years ago · Santiago Trujillo Relatório

0

Mr. Llama already corectly answered the question, this only for a quick reference what happens for different "combinations". The cmd0 is an "command" with zero-exit status and the cmd1 is with nonzero.

cmd0() { echo -n "[$@-0]"; return 0; }
cmd1() { echo -n "[$@-1]"; return 1; }
second() { echo "[second]"; }

doit() { echo "case: $@"; eval "$@"; echo; }

doit 'cmd0 start && cmd0 first && second'
doit 'cmd0 start && cmd0 first || second'
doit 'cmd0 start || cmd0 first && second'
doit 'cmd0 start || cmd0 first || second'

doit 'cmd0 start && cmd1 first && second'
doit 'cmd0 start && cmd1 first || second'
doit 'cmd0 start || cmd1 first && second'
doit 'cmd0 start || cmd1 first || second'

doit 'cmd1 start && cmd0 first && second'
doit 'cmd1 start && cmd0 first || second'
doit 'cmd1 start || cmd0 first && second'
doit 'cmd1 start || cmd0 first || second'

doit 'cmd1 start && cmd1 first && second'
doit 'cmd1 start && cmd1 first || second'
doit 'cmd1 start || cmd1 first && second'
doit 'cmd1 start || cmd1 first || second'

produces:

case: cmd0 start && cmd0 first && second
[start-0][first-0][second]

case: cmd0 start && cmd0 first || second
[start-0][first-0]

case: cmd0 start || cmd0 first && second
[start-0][second]

case: cmd0 start || cmd0 first || second
[start-0]

case: cmd0 start && cmd1 first && second
[start-0][first-1]

case: cmd0 start && cmd1 first || second
[start-0][first-1][second]

case: cmd0 start || cmd1 first && second
[start-0][second]

case: cmd0 start || cmd1 first || second
[start-0]

case: cmd1 start && cmd0 first && second
[start-1]

case: cmd1 start && cmd0 first || second
[start-1][second]

case: cmd1 start || cmd0 first && second
[start-1][first-0][second]

case: cmd1 start || cmd0 first || second
[start-1][first-0]

case: cmd1 start && cmd1 first && second
[start-1]

case: cmd1 start && cmd1 first || second
[start-1][second]

case: cmd1 start || cmd1 first && second
[start-1][first-1]

case: cmd1 start || cmd1 first || second
[start-1][first-1][second]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda