Shell Command Structure Summary
Positional Arguments
Positional arguments are passed as-is after the command name.
cat file1.txt file2.txt
Options (Named Arguments)
Options take values and come in two types:
-
Short: One-letter options preceeded by a
-
. Values are placed after (usually separated by a space).git commit -m "Fix"
-
Long: Multi-letter options preceeded by a
--
. Values are placed after (separated by=
or a space).git commit --message="Fix"
=git commit --message "Fix"
Flags
Flags don't take a value and can be long or short.
git commit --dry-run -v
Combining Short Flags
When using multiple short flags, you can combine them into one.
ls -a -g -h
= ls -agh
Ordering
The order of options/flags does not matter.
ls -a -g
= ls -g -a
However, some commands require all options/flags to come before positional arguments.
FAILS: ls my-dir -a
WORKS: ls -a my-dir
Thankfully, most modern commands are agnostic.
git add a.js -v
= git add -v a.js
I prefer to put positional arguments before options/flags when possible, as I find it easier easier to read.
Quotes
Quotes are only required for strings that contain special characters (e.g. spaces).
FAILS: git commit -m Fix thing
WORKS: git commit -m "Fix thing"
WORKS: git commit -m Fix
Note: When writing npm scripts, prefer double quotes
"
to single'
for better cross-compatability with Windows.
Subcommands
Many modern commands use a command-subcommand structure, like git. In the following example, git
is the root command, and commit
is the subcommand.
git commit -m "Hi"