基础知识

发布于 2026-07-25

命令参数语法

Command Line Interface Conventions

Usage 语法说明

12. Utility Conventions

Unix/Linux 命令帮助(--help)通常采用一种约定俗成的语法描述命令格式。理解这些符号后,大多数命令的 Usage 都可以快速读懂。

语法含义示例
[]可选参数(Optional)[-a]
<>占位符(有些程序使用)<file>
...可以出现多个file...
``二选一json | xml
--选项结束rm -- -file
-a短参数tree -a
--all长参数git --version
[directory ...]可指定多个目录tree dir1 dir2 dir3

以 tree 命令为例,在 macOS 或者 Linux 上输入 tree --help 命令,会得到如下帮助信息:

tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-L level [-R]] [-H [-]baseHREF]
	[-T title] [-o filename] [-P pattern] [-I pattern] [--gitignore]
	[--gitfile[=]file] [--matchdirs] [--metafirst] [--ignore-case]
	[--nolinks] [--hintro[=]file] [--houtro[=]file] [--inodes] [--device]
	[--sort[=]name] [--dirsfirst] [--filesfirst] [--filelimit[=]#] [--si]
	[--du] [--prune] [--charset[=]X] [--timefmt[=]format] [--fromfile]
	[--fromtabfile] [--fflinks] [--info] [--infofile[=]file] [--noreport]
	[--hyperlink] [--scheme[=]schema] [--authority[=]host] [--opt-toggle]
	[--compress[=]#] [--condense] [--version] [--help]
	[--] [directory ...]

[] 表示什么

例如:

[-a]

表示:-a 可以写,也可以不写,因此,下面都合法:

tree -a
tree

[-L level [-R]]

意思是:

  • -L 是可选
  • level 是必须提供的参数
  • -R 又是可选

因此,下面都合法:

tree -L 1
tree -L 1 -R
tree -L 1 -R -a

[directory ...]

表示:可以提供 0 个、1 个或多个目录,例如:

# 当前目录
tree
 
# 指定一个目录
tree src
 
# 指定多个目录
tree src docs

--

表示:后面的内容全部认为是文件名,而不是参数。假如目录叫 -abc:

tree -- -abc

否则,

tree -abc

程序会误认为:

-a
-b
-c

是三个参数,而不是一个目录名。