Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
ignoring specific exit code in bash

Note: Question does not duplicate Ignoring specific errors in a shell script .


Suppose it is needed to capture the leading characters of a encoded representation of a file.

In shell (tested in Bash), it is easy to use the following form:

encoded="$(< file base64 | head -c16)"

The statement functions desired, except under certain alterations to the environment.

Consider the following:

set -o errexit -o pipefail
shopt -s inherit_errexit
encoded="$(< file base64 | head -c16)"

The final line would cause termination of a script, because of the non-zero return status (141) given by base64, unhappy with closed pipe. The return status is propagated to the pipe and then to the invoking shell.

The undesired effect requires a workaround, such as follows:

set -o errexit -o pipefail
shopt -s inherit_errexit
encoded="$((< file base64 || :) | head -c16)"

The : has the same effect as would have the keyword true, to evaluate as a non-error.

However, this approach leads to a further unwanted effect.

The following shows a variation with a different error:

set -o errexit -o pipefail
shopt -s inherit_errexit
encoded="$((< /not/a/real/file base64 || :) | head -c16)"
echo $?

The printed code is zero. Now, a true error has been masked.

The most obvious solution, as follows, is rather verbose

set -o errexit -o pipefail
shopt -s inherit_errexit
encoded="$((< /not/a/real/file base64 || [ $? == 141 ]) | head -c16)"
echo $?

Is a more compact form available? Is any environment alteration available such that statements masks only the particular status code, without the explicit inline expression?

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda