Ir para o conteúdo principal
kitlocal.dev

Cheatsheet do Bash

Roda localmente

100% processado no seu navegador. Entradas e saídas nunca saem do seu dispositivo.

Scripts em GNU Bash e atalhos interativos: expansão de parâmetros, arrays, laços, condicionais, redirecionamentos e controle de jobs testados no Bash 5.3.

Como funciona

Bash parses a line in phases: expansions ($var, $(cmd), globs) happen before the command runs, and whitespace splits unquoted expansions into separate words. Double-quoting "$var" keeps spaces intact while still expanding variables; single quotes disable all expansion.

Kit de sobrevivência

Os principais comandos para aprender primeiro.

41 comandos

Atalhos de teclado/Focar buscaJKMover seleçãoCCopiar selecionadoFFavoritar selecionadoEscLimpar busca

Script safety & quoting

Start scripts with strict error checking and quote variable expansions to prevent silent failures.

Documentação oficial: The Set Builtin
  • Essencial

    Exit on errors, unset variables and failed pipeline commands.

    set -euo pipefail

    Quando usar

    Prevents scripts from charging ahead after an earlier failure or blank variable expansion.

    Detalhamento

    -e
    exit immediately when a command returns non-zero
    -u
    treat referencing an unset variable as an error
    -o pipefail
    fail a pipeline if any stage fails, not just the last

    Exemplo

    Place `set -euo pipefail` at the top of scripts to fail fast with exit code 1.

    bash -c 'set -euo pipefail; echo "$UNSET_VAR"'

    Resultado

    bash: line 1: UNSET_VAR: unbound variable

    Atenção

    • Guard commands expected to return non-zero (like grep with no matches) with `|| true` or an `if` check.

    Veja também

Variables & parameter expansion

Use ${...} parameter expansions for defaults, slicing and path stripping without spawning external tools.

Documentação oficial: Shell Parameter Expansion

Conditionals & tests

Prefer [[ ... ]] over [ ... ] in Bash scripts: it avoids word splitting and supports regex matching with =~.

Documentação oficial: Conditional Constructs

Loops & safe file reading

Never parse `ls` in a `for` loop. Use globs for filenames and `while IFS= read -r` for lines.

Documentação oficial: Looping Constructs

Redirection, pipes & process substitution

FD 1 is stdout (normal output), FD 2 is stderr (errors). Order matters when combining them.

Documentação oficial: Redirections

Functions, shortcuts & job control

Use readline shortcuts in your terminal and background/foreground jobs for long tasks.

Documentação oficial: Job Control

Experimente em uma ferramenta do KitLocal

Glossário
Word splitting
Unquoted $var expansions split on spaces/tabs/newlines (IFS), breaking filenames with spaces.
Exit status ($?)
Integer 0..255 returned by the last command. 0 means success; any non-zero value means failure.
Parameter expansion
Transforming variables inside ${...} without spawning sed/awk subprocesses.
Subshell
A child shell created by (...) or pipes where variable changes do not affect the parent shell.

Verificado em 15 de nov. de 2026 · Verificado na versão GNU bash 5.3

Fontes: GNU Bash Reference Manual · ShellCheck Wiki (common pitfalls)

Substitua qualquer item entre colchetes angulares pelos seus próprios valores. Os comandos vêm da documentação oficial e os exemplos foram executados na versão indicada.