Shell 命令格式
shell 命令由三部分组成:命令,选项和参数。它们的格式为:
command [option] [argument]
其中,选项用于调整命令的行为,通常以 “-” 或 “–” 开头,也有直接以字母开头的选项,不过这种选项不多见;参数用于指定命令要处理的目标。选项和命令都属于可选的,有些情况下,我们可以单独执行命令。
示例:
it@workstation:~$ ls -l /home/
total 4
drwxr-xr-x 17 it it 4096 1月 28 13:49 it
其中,ls 就是命令,-l 为选项,/home/ 则是参数;
在 Linux 中,存在三种选项风格:
短选项风格: 来自 Unix,如前面的示例中的 -l 这就是一个短选项;
长选项风格: GNU 风格选项,如 --human;
很多命令都同时支持长选项和短选项,并且,每个短选项,都有一个与之对应的长选项:
it@workstation:~$ free -h
total used free shared buff/cache available
Mem: 1.9Gi 746Mi 134Mi 12Mi 1.1Gi 1.0Gi
Swap: 2.0Gi 0.0Ki 2.0Gi
it@workstation:~$ free --human
total used free shared buff/cache available
Mem: 1.9Gi 746Mi 134Mi 12Mi 1.1Gi 1.0Gi
Swap: 2.0Gi 0.0Ki 2.0Gi
从上面的命令结果,我们可以看出,对于 free 命令,-h 等同于 --human;
BSD 风格: 在命令后面直接加选项,如,a;
it@workstation:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.5 167604 11460 ? Ss 11:06 0:01 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 11:06 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 11:06 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 11:06 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 11:06 0:00 [kworker/0:0H-kblockd]
root 8 0.0 0.0 0 0 ? I< 11:06 0:00 [mm_percpu_wq]
root 9 0.0 0.0 0 0 ? S 11:06 0:00 [ksoftirqd/0]
root 10 0.0 0.0 0 0 ? I 11:06 0:02 [rcu_sched]
... ... ... ...
... ... ... ...
* 其中 aux 为 3 个选项;
如果命令中有多个选项,我们可以将它们写到一起:
it@workstation:~$ ls -l -d /etc/
drwxr-xr-x 129 root root 12288 1月 28 11:13 /etc/
it@workstation:~$ ls -ld /etc/
drwxr-xr-x 129 root root 12288 1月 28 11:13 /etc/