C.W.K.
Stream
Lesson 01 of 10 · published

Standard Stream

~12 min · stdin, stdout, stderr, fd

Level 0창 구경꾼
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

모든 프로그램이 가진 세 문

Unix 의 모든 프로세스는 태어날 때 세 file descriptor 를 들고 있어: stdin (0), stdout (1), stderr (2). kernel 과 shell 이 프로그램과 대화하는 통로야. 파이프와 redirection 은 결국 이 세 문을 다른 곳에 연결하는 것뿐이야.

기본 연결

  • stdin (0) — 키보드.
  • stdout (1) — 터미널 화면.
  • stderr (2) — 마찬가지로 터미널 화면이지만 분리된 스트림. 에러가 파이프 데이터에 안 섞이게.

왜 출력 스트림이 두 개?

find / -name foo 2>/dev/null | grep bar 를 보면 — 에러 ('Permission denied') 는 stderr 로 가서 2>/dev/null 로 잠재우고, 실제 매치는 파이프 타고 grep 으로 가. find 에 스트림이 하나뿐이면 노이즈와 데이터를 분리 못 해. 이 분할이 Unix 설계 최고의 결정 중 하나야.

숫자 핸들

Redirection 에 숫자 자주 봐: 2> (stderr), 1> (stdout, 기본), 2>&1 (stderr 를 stdout 에 합침). 이게 file descriptor 번호 — kernel 직속 핸들. 새 fd 도 만들 수 있어: exec 3> file 로 fd 3 열고 echo hello >&3 으로 쓸 수 있어.

Code

세 스트림 직접 보기·bash
# stdout vs stderr — both show up by default
echo 'hello'                    # stdout
ls /no/such/dir                 # stderr only
# Silence stderr
ls /no/such/dir 2>/dev/null
# Silence stdout, keep stderr
ls /etc 1>/dev/null
# Merge stderr into stdout (then pipe both)
ls /etc /no/such/dir 2>&1 | head

External links

Exercise

ls /etc /no/such/dir 실행 — stdout / stderr 둘 다 화면에 떠. ls /etc /no/such/dir > out.txt 2> err.txt 후 두 파일 읽기. 분리 성공 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.