DEV Community

Marcelo Andrade R.
Marcelo Andrade R.

Posted on • Originally published at marceloandrader.github.io

Bash sin sorpresas

Existe una línea de código que agrego siempre a los script bash
que me ayuda mucho a evitar errores comunes cuando se escribe
un script.

#!/bin/bash

set -euxo pipefail
Enter fullscreen mode Exit fullscreen mode

La tercera línea en este ejemplo sirve para indicar al interprete
lo siguiente:

set -e Le dice a bash que termine inmediatamente si algun comando falla

set -o pipefail Bash normalmente revisa el último código de salida cuando se ejecuta en pipes. Con este cambio el comportamiento hace que si un comando falla en un pipeline todo el pipeline falle.

set -u Esta opción hace que el interprete emita un error y salga si una variable no esta definida.

set -x Esta es mi favorita porque permite imprimir en la consola cada comando antes de ejecutar. Los argumentos son expandidos.

Si desean ver ejemplos de cómo funciona por favor revisar:

Safer bash scripts with set -euxo pipefail

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay