目次

Bash(Bourne-Again Shell)

シェバン(shebang)

bash を絶対パスを指定

#!/bin/bash

env を使ってを指定

#!/usr/bin/env bash

source されたスクリプトのディレクトリパスの取得

script_dir=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd)

source されていない場合は以下でも取得できる

script_dir=$(cd $(dirname $0); pwd)

alias の設定方法

.bashrc に以下のように設定する。

alias mysql='/usr/local/mysql/bin/mysql'
alias mysqladmin='/usr/local/mysql/bin/mysqladmin'

記述が反映することを確認する。

$ source .bashrc
$ alias
alias mysql='/usr/local/mysql/bin/mysql'
alias mysqladmin='/usr/local/mysql/bin/mysqladmin'

端末起動時のプロンプト表示が遅い

pk-command-not-found で時間がかかって bash の起動に時間がかかる。
.bashrc に unset command_not_found_handle を追記する。

$ vi ~/.bashrc
# .bashrc
 
# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
 
unset command_not_found_handle

.bashrc をログイン時に自動で読み込ませる

.bash_profile に以下の記述を追加する。

if [ -f ~/.bashrc ]; then
       . ~/.bashrc
fi

変更を即時反映するには、以下のコマンドを実行する。

$ source .bash_profile

参考文献

https://www.tohoho-web.com/ex/shell.html
https://atmarkit.itmedia.co.jp/ait/spv/1811/21/news009.html
シバン(Unix)
bash/zshでsourceされたスクリプト内で、ファイル自身の絶対パスをとるシンプルな記法
Fedora 14 でコマンドを打ち間違えると・・・
bashの`export`の役割と`exec $SHELL -l`について補習する - Lambdaカクテル
if 文と test コマンド | UNIX & Linux コマンド・シェルスクリプト リファレンス
引数を処理する | UNIX & Linux コマンド・シェルスクリプト リファレンス