Sobes.tech
Middle

Ключи set в bash-скриптах для дебага (set -x, set -e и т.д.)

sobes.tech AI

Answer from AI

In bash, the following set command options are often used for debugging and controlling script behavior:

  • set -x — enables command tracing. Each command is printed to stdout before execution with variable substitutions. Helps understand what is being executed.
  • set +x — disables tracing.
  • set -e — the script immediately exits upon an error (if a command returns a non-zero exit code). Useful for preventing continuation after errors.
  • set -u — causes an error when using undeclared variables.
  • set -o pipefail — if any command in a pipeline fails, the pipeline returns the error code of the failed command instead of the last command.

Example of usage for debugging:

set -euxo pipefail
# -e: exit on error
# -u: error on undeclared variables
# -x: tracing
# -o pipefail: errors in pipelines

some_command | another_command

This helps quickly identify errors and understand the flow of script execution.