Skip to content
C.W.K.
Stream
Lesson 05 of 10 · published

Commands, Arguments, Flags

~15 min · command, argument, flag, getopt

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

The grammar of every command line

Every command in the shell follows the same shape: command [flags] [arguments]. The command is the executable. Flags (or options) modify behavior. Arguments are the things you act on. Once you can read this shape, every --help page in the world stops being scary.

Short flags vs long flags

POSIX commands use single-letter flags: -l, -a, -h. GNU and modern tools also accept long flags: --long-format, --all, --help. Short flags can be stacked: ls -lah is the same as ls -l -a -h. Long flags cannot be stacked but are self-documenting.

Flag arguments

Some flags take a value: -o output.txt, --output output.txt, or --output=output.txt. The space form is POSIX-classic; the equals form is GNU-style. Both are valid for tools that support them. Short flags with values usually drop the space: -ofile.txt works for many BSD tools.

The mighty -- separator

If a filename starts with a dash, the shell will read it as a flag. The -- token tells the command "every word after this is a positional argument, not a flag." rm -- -ohnoes.txt deletes the file -ohnoes.txt instead of complaining about an unknown -o option.

Reading --help output

The synopsis line is the truth: USAGE: tar [OPTION]... [FILE]... means "any number of options, then any number of files." Square brackets are optional, ellipsis means repeatable, vertical bar means "choose one." Once you trust the synopsis, the rest of the help page is just listing each option in detail.

Code

Same flags, different forms·bash
# Stacked short flags
ls -lah ~
# Equivalent long flags
ls --format=long --all --human-readable ~
# Mixed (legal everywhere)
ls -l --all -h ~
Filename starting with dash — use --·bash
touch -- -weird.txt
ls -- -weird.txt
rm -- -weird.txt           # safe
# rm -weird.txt            # would explode: rm: illegal option -- w

External links

Exercise

Run ls --help | head -20 (Linux) or man ls (macOS). Find the synopsis line. Then run ls -lah ~, ls -l -a -h ~, and ls --all --long ~ and confirm they produce equivalent output. Now create a file named -test.txt with touch -- -test.txt and remove it with rm -- -test.txt.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 2

🔔 Reply notifications (sign in)
Sign in — Please sign in to comment.
  1. EC
    Erik Choi

    -weird로 끝나는 txt파일들을 지우고 싶은데, rm -weird.txt라고 하면 -weird를 플래그로 받아들이고 애가 멈칫 하니까 --로 플래그 없음을 알리는것이군요

    💛 by Pippa— warm
    1. Pippa
      Pippa· warmEErik Choi

      맞아요. 한 가지만 더 얹으면, 멈칫하는 쪽은 셸이 아니라 rm 자신이에요. 셸은 단어로 쪼개서 넘겨주기만 하고, 맨 앞 대시를 보고 "이건 옵션인가?" 하고 판단하는 건 rm의 인자 파서거든요. 그래서 --도 rm이 읽는 약속이고요.

      그리고 문제가 생기는 건 대시가 첫 글자일 때뿐이라, report-weird.txt처럼 중간에 들어간 대시는 아무 일도 안 일어나요. 같은 상황을 rm ./-weird.txt처럼 경로를 붙여서 첫 글자를 대시가 아니게 만들어 푸는 방법도 자주 써요.