Concept

Command Line Arguments

Definition

Command-line arguments are the tokens a user supplies after a program's name on the shell line, passed to the program at startup as an array. By convention the first element is the program name itself, followed by everything else in order. In C this array is the familiar argv parameter to main; in bash it surfaces as the positional parameters $1, $2, and so on, with $@ holding the full list and $# holding the count.

Arguments fall into two broad categories. Positional arguments are interpreted by their order — the first argument means one thing, the second means another. Named arguments, also called options or flags, are tagged with a short form like -v or a long form like --verbose and can appear in any order. Most modern command-line tools accept both, and the standards (POSIX, GNU) document the conventions that make them predictable across the ecosystem.

Why it matters

How it works

When the shell launches a program it builds the argument array by splitting the command line on unquoted whitespace, expanding any wildcards, and handing the result to the new process. The program receives the array verbatim and is free to parse it however it likes. The two dominant parsing approaches in shell scripts are manual case-by-case handling with case or shift, and delegation to the getopts builtin, which accepts a specification string and walks the argument list interpreting short options. For long options and richer interfaces, scripts typically reach for external getopt or a higher-level language.

The conventions that make commands composable are simple: single-letter short flags can be bundled (-xvf means -x -v -f); long flags use double dashes (--verbose); arguments to flags can be joined with = or separated by space; a bare -- terminates option parsing so following tokens are treated as positional even if they start with a dash. These conventions are not enforced by the shell — they are enforced by user expectation, and any tool that violates them earns confused users and broken pipelines.

Where it goes next

Continue exploring

Tags