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

Command, Argument, Flag

~15 min · command, argument, flag, getopt

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

모든 command line 의 문법

shell 의 모든 command 는 같은 모양이야: command [flags] [arguments]. command 는 실행 파일, flag (option) 는 동작 변경자, argument 는 동작 대상. 이 모양 한 번만 익히면 어떤 --help 도 안 무서워져.

Short flag vs long flag

POSIX 도구는 한 글자 flag: -l, -a, -h. GNU / 모던 도구는 long flag 도 지원: --long-format, --all, --help. Short flag 는 쌓을 수 있어: ls -lah = ls -l -a -h. Long flag 는 못 쌓지만 self-documenting 이라 가독성 좋아.

값 받는 flag

일부 flag 는 값을 받아: -o output.txt, --output output.txt, 또는 --output=output.txt. 공백형은 POSIX-classic, 등호형은 GNU 스타일. 도구가 지원하면 둘 다 OK. BSD 계열 short flag + 값은 띄어쓰기 빼는 경우 많아 — -ofile.txt.

-- separator 의 위력

파일명이 dash 로 시작하면 shell 은 그걸 flag 로 읽어. -- 토큰은 '이 뒤로는 다 positional argument 야' 라는 신호. rm -- -ohnoes.txt-ohnoes.txt 라는 파일을 지우고, 모르는 -o option 이 라고 에러내지 않아.

--help 읽는 법

synopsis 줄이 진실 — USAGE: tar [OPTION]... [FILE]... 은 'option 임의 개수, file 임의 개수'. 대괄호는 optional, … 는 반복 가능, | 는 둘 중 하나. synopsis 만 믿으면 그 아래 옵션 설명은 그냥 사전이야.

Code

같은 flag, 다른 표현·bash
# Stacked short flags
ls -lah ~
# Equivalent long flags
ls --format=long --all --human-readable ~
# Mixed (legal everywhere)
ls -l --all -h ~
Dash 로 시작하는 파일명 — `--` 로 보호·bash
touch -- -weird.txt
ls -- -weird.txt
rm -- -weird.txt           # safe
# rm -weird.txt            # would explode: rm: illegal option -- w

External links

Exercise

리눅스면 ls --help | head -20, macOS 면 man ls 실행. synopsis 줄 찾기. 그 다음 ls -lah ~, ls -l -a -h ~, ls --all --long ~ 출력이 같은지 확인. 마지막으로 touch -- -test.txt 로 파일 만들고 rm -- -test.txt 로 지워봐.

Progress

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

댓글 0

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

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