What is a Flag in a Terminal Command?
A flag in a terminal command is an optional parameter that begins with only one dash (-) and has only one letter, e.g. in ls -a -l, the name of the program is ls, and -a and -l are the flags. Flags generally can be combined together, for example: ls -al means the same thing as ls -a -l. This is only possible to parse because all flags are only a single letter in length, so when the program sees that an argument starts with a dash (-), every single letter after the dash is interpreted as a separate single-letter flag.
The order of the flags typically doesn't matter, only whether they're present, so ls -al and ls -la are equivalent.
Flags are often abbreviations of optional parameters. For example, if you check ls --help, you'll see -a is an abbreviation for --all, and it's supposed to include hidden files (which is how dotfiles are treated on Linux by convention). On the other hand, -l isn't an abbreviation for any long form option, it's just a flag to "use a long listing format" when displaying files in a directory. Observe that while single-letter flags start with a single -, long form optional parameters start with two dashes (--). This is also by convention.
Flags work like this by convention. A program can parse the arguments it gets from the operating system in any way it wishes, so it's possible that in some programs flags don't work the way they work in most programs, e.g. you can't combine flags in them. However, in most programs meant to be used from the terminal in Unix-based operating systems, the convention of flags is followed. On Windows, sometimes you see flags that start with a forward slash / instead of a dash (-)1, e.g. /? is the equivalent of -h, and dir works like ls -l by default, so dir /w (for "wide" format) makes it behave like ls without the -l flag.
Note: terminal command flags shouldn't be confused with bit flags. With bit flags, you have a structure like 00, and each bit is a separate option. A good example are RWX file permissions on Linux. Read, Write, and eXecute are three bits: 000, so rw- would be 110, which, if you read it as a binary number, becomes 6 in decimal. When you see the octal notation 660, or 777, this is where it comes from.
Observations
Personally, I really dislike these flags, specially when you find a command in a tutorial and it just says "hey, just run ln -shf foo bar" without even telling what are these flags supposed to mean.
When I write Bash scripts (or rather, Python scripts that run terminal commands, because that's much easier than writing raw Bash), I often add a comment before anything that uses flags so I don't need to keep looking up what this single letter is supposed to mean.
For the record, -shf replaces a symlink to a directory in BSD,
Quotes
In the original Unix tradition, command-line options are single letters preceded by a single hyphen.
[...]
The GNU double-hyphen option leader was chosen so that traditional single-letter options and GNU-style keyword options could be unambiguously mixed on the same command line. Thus, if your initial design has few and simple options, you can use the Unix style without worrying about causing an incompatible ‘flag day’ if you need to switch to GNU style later on. On the other hand, if you are using the GNU style, it is good practice to support single-letter equivalents for at least the most common options.
The Art of Unix Programming, "© 2003 Eric S. Raymond" [http://www.catb.org/esr/writings/taoup/html/ch10s05.html] (accessed 2024-11-27)
References
- https://superuser.com/questions/591045/what-are-the-slashes-with-letters-b-m-e-p-used-for (accessed 2024-11-27) ↩︎