LoginSignup
18
23

More than 3 years have passed since last update.

Linux系システムのコマンドラインオプションについて調べたメモ

Last updated at Posted at 2019-03-30

概要

この記事は、Linux系システムのコマンドオプションの指定の仕方を調べたときの簡単なまとめで、前半は調べたことの要約、後半は調べたときに読んだサイトの要旨の引用とその翻訳(Google翻訳)になります。

コマンドの動作確認はDocker Desktop for Windows上のUbuntuイメージで行いました。

環境

  • Windows 10 Professional
  • Docker Desktop for Windows 2.0.0.3
    • Ubuntu 18.04.2 (Docker Official Images)

コマンドオプションについて

コマンドの引数がハイフン---で始まる場合、その引数はオプションになります。(なお、引数が-から始まる場合、オプションリストの終わりを意味する--というオプションを使用します。)

Linux系システムのコマンドオプションは大別すると、ハイフン1つとハイフン2つのスタイルがあります。
通常、ハイフン1つのオプション名は1文字からなる英数字で、ハイフン2つのオプション名は1から3語程度の英単語をハイフンで区切ったものです。
(昔はハイフン1つで複数文字列のオプションもあったようです。)

-v--verbose(または--version)のように、オプションが同じ意味を持ちハイフン以降の1文字目が同じ場合、それぞれshort nameとlong nameのような関係といえますが、必ずしもこのようなルールでオプション名が決められているわけではありません。
また、ハイフン1つと2つのスタイルが必ずセットになっているということでもなく、たとえばlsコマンドの--full-timeのようにハイフン1つのスタイルがないオプションもあります。(その逆もあります。)

ハイフン1つ (one hyphen)のスタイル

POSIX標準 (POSIX standard)で決められた昔からあるスタイルで、オプション名は1文字の英数字で付けられています。
(tarのように-?が使えるコマンドもあります。)

オプションが引数(以後、オプション引数)を取らない場合は、オプションを連結して指定することができます。

$ ls -l -a -F
$ ls -laF

オプションがオプション引数を取る場合、オプションとオプション引数を半角スペースで区切るかオプションに繋げて指定します。

$ ls -w 100
$ ls -w100

オプション引数を取るオプションは、最後に指定する場合に限ってオプションどうしを連結することができるようですが、通常は単独で指定します。

$ ls -tr -w 100
$ ls -trw100

これはエラーになります。

$ ls -w100tr
ls: invalid line width: '100tr'

ハイフン2つ (two hyphens)のスタイル

GNU拡張 (GNU extension)で決められた比較的新しいスタイルで、オプション名は人間に読みやすい英単語で付けられています。通常は1から3語程度の単語をハイフンで区切った名前になっています。

このスタイルのオプションはハイフン2つから始まるルールなので、引数を取らないハイフン1つのオプションが連結している場合と区別できるようになっています。

$ ls -laF --sort=none

このスタイルのオプションがオプション引数を取る場合、オプション引数が必須か任意かでオプション引数の指定方法が少し異なります。

オプション引数が必須の場合

--sortオプションはオプション引数を指定する必要があります。オプション引数は半角スペースか=で区切ります。

$ ls -laF --sort none
$ ls -laF --sort=none

オプション引数が任意の場合

--colorオプションはオプション引数の指定は任意です。指定しなかった場合はデフォルトの'always'が使われます。
この場合、オプション引数を指定する場合は必ず=で区切る必要があります。

$ ls -laF --color
$ ls -laF --color=always

引数がハイフンから始まる場合

引数がハイフンからはじまる場合、そのまま指定するとコマンドからオプションとして認識されてしまいます。
たとえば、次のようにハイフンから始まるファイルを作成しようとするとエラーとなります。

$ touch -abc.txt
touch: invalid option -- 'b'
Try 'touch --help' for more information.

-abc.txtがオプションとして扱われないようにするには、オプションリストの終わりを示す--を使用します。

$ touch -- -abc.txt
$ ls -1 -- -abc.txt
-abc.txt

ps コマンド

psコマンドのオプションには、ハイフンありのもの(System V Style or AT&T Style)と、無しのもの(BSD Style)があり、psコマンドはこの2つのスタイルを取り入れています。

下記はman psのDESCRIPTIONより引用しました。

This version of ps accepts several kinds of options:

1 UNIX options, which may be grouped and must be preceded by a dash.
2 BSD options, which may be grouped and must not be used with a dash.
3 GNU long options, which are preceded by two dashes.

System V Style

ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:37 pts/0    00:00:00 bash
root        51     1  0 11:59 pts/0    00:00:00 ps -ef
ps -e
  PID TTY          TIME CMD
    1 pts/0    00:00:00 bash
  108 pts/0    00:00:00 ps
  • -e : Select all processes. Identical to -A.
  • -f : Do full-format listing.

BSD Style

ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   6032  3160 pts/0    Ss   11:37   0:00 bash
root        52  0.0  0.0  15980  2712 pts/0    R+   12:00   0:00 ps aux
ps ax
  PID TTY      STAT   TIME COMMAND
    1 pts/0    Ss     0:00 bash
  107 pts/0    R+     0:00 ps ax
  • a : this option causes ps to list all processes with a terminal (tty), or to list all processes when used together with the x option.
  • x : this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the a option.
  • u : Display user-oriented format.

参考にしたサイト

Command-line interface - Wikipedia より

Option conventions in Unix-like systems

one hyphen

the old convention (and still available as an option for frequently-used options) is to use one hyphen then one letter (e.g., -c);

古い慣習(そしてそれでもなお頻繁に使用されるオプションのオプションとして利用可能です)は、ハイフンを1つと1文字を使用することです(例えば、-c)。

two hyphens

the new (and GNU) convention is to use two hyphens then a word (e.g. --create) to identify the option's use

新しい(そしてGNUの)慣習は、オプションの使用を識別するために2つのハイフンと単語(例えば--create)を使うことです。

getopt - Wikipedia より

getopt

getopt is a C library function used to parse command-line options.
It is also the name of a Unix program for parsing command line arguments in shell scripts.

getoptは、コマンドラインオプションを解析するために使用されるCライブラリ関数です。 シェルスクリプトでコマンドライン引数を解析するためのUnixプログラムの名前でもあります。

A GNU extension, getopt_long

A GNU extension, getopt_long, allows parsing of more readable, multicharacter options, which are introduced by two dashes instead of one. The choice of two dashes allows multicharacter options (--inum) to be differentiated from single character options specified together (-abc).

GNU拡張機能のgetopt_longを使うと、より読みやすい多文字オプションを解析できます。これは、1つではなく2つのダッシュで始まります。 2つのダッシュを選択すると、複数文字オプション(--inum)を、一緒に指定された単一文字オプション(-abc)と区別することができます。

The GNU C Library より

25.1.1 Program Argument Syntax Conventions

POSIX recommends these conventions for command line arguments.

Arguments are options if they begin with a hyphen delimiter (-).

引数がハイフン区切り文字(-)で始まる場合、引数はオプションです。

Multiple options may follow a hyphen delimiter in a single token if the options do not take arguments. Thus, -abc is equivalent to -a -b -c.

オプションが引数をとらない場合、複数のオプションが単一トークン内のハイフン区切り文字の後に続くことがあります。 したがって、-abc-a -b -cと同じです。

Option names are single alphanumeric characters.

オプション名は単一の英数字です。

An option and its argument may or may not appear as separate tokens. (In other words, the whitespace separating them is optional.) Thus, -o foo and -ofoo are equivalent.

オプションとその引数は、別々のトークンとして表示される場合と表示されない場合があります。(つまり、それらを区切る空白は任意です。)したがって、-o foo-ofooは同等です。

GNU adds long options

GNU adds long options to these conventions. Long options consist of -- followed by a name made of alphanumeric characters and dashes. Option names are typically one to three words long, with hyphens to separate words. Users can abbreviate the option names as long as the abbreviations are unique.

GNUはこれらの規約に長い選択肢を追加します。 長いオプションは、--の後に英数字とダッシュで構成される名前が続きます。 オプション名は通常1〜3語の長さで、ハイフンで単語を区切ります。 短縮形が一意である限り、ユーザーはオプション名を短縮することができます。

To specify an argument for a long option, write --name=value. This syntax enables a long option to accept an argument that is itself optional.

長いオプションの引数を指定するには、 --name=valueと書きます。この構文は、長いオプションがそれ自体がオプションである引数を受け入れることを可能にします。

GNU tar: an archiver tool より

3.3 The Three Option Styles

Long Option Style

Each option has at least one long (or mnemonic) name starting with two dashes in a row, e.g., --list. The long names are more clear than their corresponding short or old names.

各オプションは、一行に2つのダッシュで始まる少なくとも1つの長い(またはニーモニック)名前、例えば --listを持ちます。 長い名前は、対応する短い名前や古い名前よりも明確です。

Short Option Style

Most options also have a short option name. Short options start with a single dash, and are followed by a single character, e.g., -t (which is equivalent to --list). The forms are absolutely identical in function; they are interchangeable.

ほとんどのオプションには短いオプション名もあります。 短いオプションは単一のダッシュで始まり、その後に単一の文字が続きます(例えば、 -t(これは--listと同等です)。 形式は機能的に全く同じです。 それらは交換可能です。

Old Option Style

As far as we know, all tar programs, GNU and non-GNU, support old options: that is, if the first argument does not start with -, it is assumed to specify option letters. GNU tar supports old options not only for historical reasons, but also because many people are used to them.

私たちの知る限りでは、GNUと非GNUのすべてのtarプログラムは古いオプションをサポートしています:つまり、最初の引数が -で始まっていなければ、オプション文字を指定するとみなされます。 GNU tarは、歴史的な理由だけでなく、多くの人々がそれらに慣れていることからも、古いオプションをサポートしています。

GNU Grep 3.3 より

2.1 Command-line Options

grep comes with a rich set of options: some from POSIX and some being GNU extensions. Long option names are always a GNU extension, even for options that are from POSIX specifications. Options that are specified by POSIX, under their short names, are explicitly marked as such to facilitate POSIX-portable programming.

grepには豊富なオプションがあります。POSIXからのものとGNU拡張機能からのものです。 POSIX仕様のオプションであっても、長いオプション名は常にGNUの拡張機能です。 POSIXで指定されたオプションは、その短縮名で、POSIX移植性のあるプログラミングを容易にするために明示的にそのようにマークされています。

curl - How to Use より

OPTIONS

single-dash

The short "single-dash" form of the options, -d for example, may be used with or without a space between it and its value, although a space is a recommended separator.

短い"一重ダッシュ"形式のオプション、たとえば-dは、その値とその値の間にスペースを入れても入れなくても使用できますが、スペースが推奨される区切り文字です。

double-dash

The long "double-dash" form, -d,--data for example, requires a space between it and its value.

たとえば、長い"二重ダッシュ"形式(-d--data)では、値と値の間にスペースが必要です。

don't need any additional values

Short version options that don't need any additional values can be used immediately next to each other, like for example you can specify all the options -O,-L and -v at once as -OLv.

追加の値を必要としない短いバージョンのオプションは、たとえば-O-L、および-vのすべてのオプションを-OLvとして一度に指定することができます。

似た質問

18
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
23