Bash
Bash
Bash (Bourne Again SHell) は,Linux や macOS などの UNIX 系 OS で広く使われているシェル環境です.コマンドの入力による操作に加え,シェルスクリプトによる自動化が可能で,開発環境やサーバ管理で重要な役割を果たします.ファイル名やコマンドは大文字・小文字を区別し,パイプやリダイレクトなどの機能も豊富です.Linux の基本操作を学ぶ上で,Bash の理解は欠かせません.
シェル環境を利用するには Ubuntu などの Linux デスクトップ環境からは「端末」アプリケーションを起動します.macOSでは「ターミナル」が標準のアプリケーションです.その他,「iTerm」のようなアプリケーションをインストールして利用しても良いでしょう.なお,「Warp」というモダンターミナルアプリも高機能です.なお,macOS では Bash に拡張機能を追加した Zsh (Z Shell) が標準になっています.
「端末」や「ターミナル」を起動すると,次のような表示になります.
rinsaka@ubuntu:~$
上の表示は【「ログイン中のユーザ名」@「端末名」:「カレントディレクトリ」$】という構造になっています.ここで,「~
」がユーザのホームディレクトリを意味することに注意してください.なお,Ubuntu では「/home/ユーザ名/
」がホームディレクトリであることが多く,macOS では「/Users/ユーザ名/
」がホームディレクトリになります.
Windows から Ubuntu に遠隔ログインする方法はこちらを参照してください.
はじめの一歩
それでは,Bash を利用していきましょう.まず,現在のワーキングディレクトリを表示するコマンドは「pwd
」(Print Working Directory) です.
rinsaka@ubuntu:~$ pwd ⏎
/home/rinsaka
rinsaka@ubuntu:~$
ワーキングディレクトリ(多くの場合カレントディレクトリと同義で,カレント・ワーキング・ディレクトリと記載されることもある)内のファイルやサブディレクトリを表示するには「ls
」(LIst directory contents) コマンドを使います.
rinsaka@ubuntu:~$ ls ⏎
Documents public_html
rinsaka@ubuntu:~$
画面を消去するには「clear
」コマンドを実行します.
rinsaka@ubuntu:~$ clear ⏎
「man
」コマンドはマニュアルを表示するためのコマンドです.例えば「pwd
」コマンドのマニュアルを参照します.表示されたマニュアルは,スペースキーで1画面分のスクロールが可能で,q で終了します.また,キーボードホームポジションの j と k で上下にスクロールできます.↓ や ↑ でも上下にスクロールできますが,Unix 系 OS のアプリケーションでは j と k の上下に加えて,h と l で左右に移動できます.これらのキーは右手のホームポジションで操作できるので,自在に操作できるように身につけると良いでしょう.
rinsaka@ubuntu:~$ man pwd ⏎
PWD(1) User Commands PWD(1)
NAME
pwd - print name of current/working directory
SYNOPSIS
pwd [OPTION]...
DESCRIPTION
Print the full filename of the current working directory.
-L, --logical
use PWD from environment, even if it contains symlinks
-P, --physical
avoid all symlinks
--help display this help and exit
--version
output version information and exit
If no option is specified, -P is assumed.
Manual page pwd(1) line 1 (press h for help or q to quit)
多くのコマンドは「--help
」オプションで利用方法を確認できます.
rinsaka@ubuntu:~$ pwd --help ⏎
pwd: pwd [-LP]
Print the name of the current working directory.
Options:
-L print the value of $PWD if it names the current working
directory
-P print the physical directory, without any symbolic links
By default, `pwd' behaves as if `-L' were specified.
Exit Status:
Returns 0 unless an invalid option is given or the current directory
cannot be read.
rinsaka@ubuntu:~$
このようにキーボードからコマンドを入力して実行していくわけですが,過去に実行したコマンドを再度実行するには,↑ や ↓ を何度か押すと良いでしょう.例えば ↑ キーを数回押すと過去に入力した「ls
」コマンドが表示されるので Enter で再び実行できます.
rinsaka@ubuntu:~$ ls ⏎
Documents public_html
rinsaka@ubuntu:~$
また,過去に実行したコマンドは「history
」コマンドで一覧表示されます.
rinsaka@ubuntu:~$ history ⏎ 1 pwd 2 ls 3 clear 4 man pwd 5 pwd --help 6 ls 7 history
「history
」コマンドで表示された一覧の中から,その番号を指定して同じコマンドを再度実行することができます.具体的には,「!
」の後に番号を入力します.例えば,次のように2の「ls
」を実行します.
rinsaka@ubuntu:~$ !2 ⏎ ls Documents public_html rinsaka@ubuntu:~$
さらに過去に実行したコマンドを文字列で検索して実行する reverse-i-search も便利です.プロンプトで Ctrl + R を押すと,表示が次のような履歴検索モードになります.
(reverse-i-search)`':
その状態で例えば p を入力すると,コマンド履歴から p が含まれるコマンドが表示されます.いまは「pwd --help
」が表示されました.このコマンドで良ければ Enter で実行できます.
rinsaka@ubuntu:~$ Ctrl + R (reverse-i-search)`p': pwd --help ⏎ rinsaka@ubuntu:~$ pwd --help pwd: pwd [-LP] Print the name of the current working directory. Options: -L print the value of $PWD if it names the current working directory -P print the physical directory, without any symbolic links By default, `pwd' behaves as if `-L' were specified. Exit Status: Returns 0 unless an invalid option is given or the current directory cannot be read. rinsaka@ubuntu:~$
このとき,中間一致の検索方法が用いられます.よって,「st
」で検索した場合でも,「history
」が検索できました.
rinsaka@ubuntu:~$ Ctrl + R (reverse-i-search)`st': history ⏎ rinsaka@ubuntu:~$ history 1 pwd 2 ls 3 clear 4 man pwd 5 pwd --help 6 ls 7 history 8 ls 9 pwd 10 history rinsaka@ubuntu:~$
Bash を終了するには,「exit
」を実行します.
rinsaka@ubuntu:~$ exit ⏎