Make money doing the work you believe in

Command-line flags—also known as options or switches—are modifiers passed to a terminal command to change its behavior. They act like configuration settings, telling the program exactly how you want it to run.

Core Syntax and Formats

Flags are always placed to the right of the base command name. They typically start with a single hyphen (-) or a double hyphen (--) to distinguish them from standard text or file targets.

  • Short-form flags: These use a single hyphen and a single letter (e.g., -h or -v). You can often combine them together under one hyphen, so running ls -a -l can be shortened to ls -al.

  • Long-form flags: These use a double hyphen and a full descriptive word (e.g., --help or --verbose). They are much easier to read when writing automated scripts.

Types of Flags

  1. Boolean Flags (Switches): These act like simple on/off toggles. They do not require extra data.

    • Example: rm -f file.txt (The -f forces deletion without asking for confirmation).

  2. Value-accepting Flags (Options): These require you to provide a specific value right after the flag.

    • Example: curl -X POST api.com (The -X flag requires you to specify the HTTP method, which is POST).

    • Example with assignment: python --version or --output=result.txt.

Real-World Examples

To see how they alter a program's output, look at the common ls (list directory) command in Unix-like systems:

  • ls → Lists basic file names.

  • ls -a → Modifies the command to reveal all files, including hidden ones.

  • ls -l → Modifies the command to display files in a long, detailed list format.

  • ls -al → Combines both behaviors to show a detailed list of all files.

Flag vs. Argument: What's the Difference?

While everything typed after a command is technically a command-line argument, they serve different roles:

  • Flags modify how the action happens (e.g., -json, --quiet).

  • Positional Arguments represent what the action happens to, such as a file path or a URL (e.g., project/index.html).

If you are developing your own software, programming languages have built-in libraries—like Python's argparse .

Jun 16
at
6:23 PM
Relevant people

Log in or sign up

Join the most interesting and insightful discussions.