5.1.1. シェルの概要/Shell overview

5.1.1.1. シェルの利用/Using the shell

コマンドラインの操作では、シェル(shell, 殻)と呼ばれる種類のプログラムを使います。 シェルに対してコマンドを入力すると、シェルがコマンドを実行します。 コマンドにはシェル自身で実行できるものと、独立のプログラムとして存在しているものがあります。 独立のプログラムに関しては、シェルはそのプログラムを実行するようにOSに依頼します。

When you use the computer through command line operation, you use a program that is called a ‘shell’. You enter commands to the shell, and the shell executes them. Commands may be either built in to the shell, or exist as a program file outside of the shell. When the shell needs to execute a program file, it will request the OS to do so.

シェルという名前は、メモリ内に常に存在するOSのコードを指す、 カーネル(kernel, 核)という名前に対比して、つけられています。 OSの本体がカーネルであり、カーネルの制御のもとで様々なアプリケーションプログラムが実行されます。 カーネルが中心にあり、その周りでプログラムが動作する、というイメージです。 プログラムのうちのひとつがシェルであり、システムの表面に存在するユーザ向けのインタフェースとして、 ユーザとの(テキスト形式での)対話を受け持ちます。

The name ‘shell’ is given in contrast to the term ‘kernel’ which refers to the OS code that stays in memory. The kernel is the OS itself, and other program runs under the control of the kernel. This is metaphorically visualized as the kernel placed in the center, and other programs running around the kernel. One of those other programs is the shell, and it is located at the surface of the computer system, to provide a text-based interface to users.

WindowsやMacOSなどの今日のOSのデスクトップ画面からシェルを使うには、 なんらかの端末プログラム(より正確には端末エミュレータプログラム)を使います。 端末プログラムの中でシェルが実行されるので、そのシェルを使います。 端末プログラムは、ウィンドウを持たないテキストベースの(文字だけの)画面のプログラムに、 仮想的なテキスト画面とキーボードを提供するウィンドウを持ったプログラムです。

To use a shell from the desktop screen of a modern OS such as Windows or MacOS, you need to use a terminal program (more specifically a “terminal emulator program”). You will use a shell will be executed within that terminal program. virtual text screen and keyboard to window-less programs. A terminal (emulator) program is a window based program that provides a

5.1.1.2. シェルの種類/shell variations

Linuxのシェルプログラムには、標準のbashの他、さらに多機能なzshなど、いくつか種類があります。 それぞれのシェルの使い方を解説した本が出ているほど多機能です。 ある程度使い慣れて、もっと便利に使いこなしたくなったら、本を読んでみることをおすすめします。 驚くような便利な機能がみつかると思います。

There are several shell variations available for Linux; the standard bash, the more powerful zsh, and some others. They have so many features such that there are books on their own. After you master the basic usage, we recommend you to read a book on the shell you use. You will probably find surprisingly useful features.

5.1.1.3. シェルが動作するディレクトリ/The working directory

シェルを使う際には、カレントディレクトリを常に意識する必要があります。 カレントディレクトリを移動させるcdコマンドは誰もが最初に覚えるコマンドの一つでしょう。

実際には、カレントディレクトリというのは、シェルの機能ではなくて プロセス の機能です。 全てのプロセスには、カレントディレクトリという(OSが覚えている)デイレクトリがあります。 ファイル名や相対パス名を指定した時には、カレントディレクトリを起点としてそのファイルが検索されます。

When working with the shell, you must be aware of the location of the working directory all the time. The cd command that changes the working directory is undoubtedly one of the first shell commands that everyone learns.

As a matter of fact, the working directory is a feature of the process, and not the shell program. Every process is associated with its own working directory, which the OS remembers. file names and relative path names are searched from this working directory.

5.1.1.4. ワイルドカードの展開/Wildcard expansion

コマンドに渡す引数のファイル名には、「*」や「?」などのワイルドカードと呼ばれる 文字を含めることができます。 ワイルドカード文字を含んだ引数を渡すと、シェルがワイルドカードを含んだ ファイル名にマッチするファイルを探し、マッチしたファイル名に展開します。

Filenames in command line arguments may contain so-called wildcard characters such as “*” or “?”. When wildcards are found in arguments, the shell will search for file names that match the filename including wildcards, and replace the argument with the found fille names.

例えば、拡張子 .c で終わるファイルを全て src ディレクトリにコピーするためには、 次のように入力します。:

To copy all files that end with extension “.c”, you would type like so:

$ cp *.c src

シェルは、ワイルドカード文字「*」を見つけ、”*.c” というパターンにマッチする ファイルを探します。 カレントディレクトリに main.c funcs.c table.c というファイルがあったとすると、 シェルはワイルドカードを含んだパターンを次のように置き換えます。:

Then, the shell would find a wildcard “*” in the path name and search for file names that match the pattern “*.c”.

If the current directory contained files with names main.c, funcs.c and table.c then the shell would replace the pattern as so:

$ cp funcs.c main.c table.c src

置き換えられる際にアルファベット順になります。

Note that the replaced result will be in alphabetical order.

ワイルドカード展開(英語では filename globbing)は、シェルの機能なので、 シェルから自作のプログラムを起動するような場合でも作用します。

Wildcard expansion (or “filename globbing”) is a feature of the shell, so it is available when invoking any program, including programs of your own.