본문으로 바로 가기
kitlocal.dev

Bash 치트시트

로컬에서 실행

100% 브라우저에서 처리됩니다. 입력과 출력이 기기 밖으로 나가지 않습니다.

GNU Bash 스크립팅 및 대화형 단축키: 매개변수 확장, 배열, 반복문, 조건문, 리다이렉션 및 Bash 5.3에서 검증된 작업 제어.

동작 원리

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.

서바이벌 키트

가장 먼저 배워야 할 몇 가지 명령어입니다.

41개 명령어

키보드 단축키/검색창 포커스JK선택 이동C선택 항목 복사F선택 항목 북마크Esc검색 지우기

Script safety & quoting

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

공식 문서: The Set Builtin
  • 기본

    Exit on errors, unset variables and failed pipeline commands.

    set -euo pipefail

    사용 시점

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

    상세 분석

    -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

    예제

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

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

    결과

    bash: line 1: UNSET_VAR: unbound variable

    주의 사항

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

    참고

Variables & parameter expansion

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

공식 문서: Shell Parameter Expansion

Conditionals & tests

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

공식 문서: Conditional Constructs

Loops & safe file reading

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

공식 문서: Looping Constructs

Redirection, pipes & process substitution

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

공식 문서: Redirections

Functions, shortcuts & job control

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

공식 문서: Job Control

KitLocal 도구에서 사용해 보기

용어집
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.

2026. 11. 15.에 확인됨 · GNU bash 5.3 기준으로 확인됨

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

꺾쇠괄호 안의 내용을 본인의 값으로 바꾸세요. 명령어는 공식 문서에서 가져왔으며, 예제는 명시된 버전에서 실행되었습니다.