Skip to content

Instantly share code, notes, and snippets.

@danny-andrews
Last active September 5, 2023 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danny-andrews/3b7cf181bbc0c33c3b410f4ebdf7e623 to your computer and use it in GitHub Desktop.
Save danny-andrews/3b7cf181bbc0c33c3b410f4ebdf7e623 to your computer and use it in GitHub Desktop.
Shell Command Structure Summary

Shell Command Structure Summary

Arguments (i.e. Positional Arguments)

Positional arguments are passed as-is after the command name. Order matters.

cat file1.txt file2.txt


Options (Named Arguments)

Options are named arguments and come in two forms:

  • Short: One-letter options are preceeded by a - with values placed after (separated by a space).

    git commit -m "Fix"

  • Long: Multi-letter options are preceeded by a -- with values placed after (separated by a = or by a space).

    git commit --message="Fix" 🟰 git commit --message "Fix"

Note: Spaces aren't required after options, but are encouranged for readability, because git commit -m"Fix" looks cramped.


Flags

Flags represent boolean values. They are options which 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 🟰 ls -gha


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.

ls my-dir -a
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 to read.


Quotes

Quotes are only required for strings that contain special characters (e.g. spaces).

git commit -m Fix thing
git commit -m "Fix thing"
git commit -m Fix

Note: When writing npm scripts, prefer double quotes (") to single (') for better cross-compatability with Windows.


Subcommands

Many modern command-line programs use a command-subcommand structure. In the following example, git is the root command, and commit is the subcommand.

git commit -m "Hi"

Summary

CLI Anatomy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment