11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bashの「command not found」をカスタムする

Last updated at Posted at 2016-05-05

存在しないコマンドを入力した時に「コマンドが見つかりません」や「comannd not found」などといったような表示が出ますが、これをカスタムする方法をメモります。

通常では、以下のように存在するコマンド( 例 echo ) を入力すると、コマンドに応じた動作をし、存在しないコマンド( ehcooooo )を入力するとエラーメッセージが返ってきます。

echo "test"
実行結果 => test

echoooooo "test"
実行結果 => command not found

この2番目の例の、コマンドが見つからなかった場合の表示を自由に変更します。

.bashrcに以下のような記述を追記します。

command_not_found_handle() {
  local cmd
  cmd=${1##*/}
  echo "\"${cmd}\"なんてコマンドありまへん。"

}

これで設定OKです。

コンソールを起動しなおすか、以下のコマンドでbashrcを読み込ませてください。

source ~/.bashrc

では、存在しないコマンドを打ってみましょう。

echoooooo "test"
実行結果 => "echoooooo"なんてコマンドありまへん。

ちゃんと、設定したエラーメッセージになりました。

.bashrcでcommand_not_found_handleの中身を変えてやればカスタムし放題です。

以上です。

11
7
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
11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?