はじめに
以下の方法では現ユーザーのログインシェルが出力されます。
$ bash
$ echo $SHELL
/bin/bash
$ zsh
$ echo $SHELL
/bin/bash
使用するシェルをzshに変更しても$SHELLにはログインシェルであるbashのフルパスが出力されます。
SHELL
The full pathname to the shell is kept in this environment variable. If it is not set when the shell starts, bash assigns to it the full pathname of the current user's login shell.
bashからzshへ変更した際にzshであることを確認する方法について記載します。
確認方法
echo $0
ターミナルでコマンドを実行する際には次のようにecho $0を実行することで確認できます。
$ bash
$ echo $0
bash
$ zsh
$ echo $0
zsh
ただしシェルスクリプトの中で使用すると期待しない結果になります。
sample.sh
echo $0
$ bash sample.sh
sample.sh
シェルではなく実行時のコマンド名が出力されます。
readlink "/proc/$$/exe"
シェルスクリプトでも現在使用しているシェルを確認するためには次のコマンドを実行します。
$ bash
$ readlink "/proc/$$/exe"
/bin/bash
$ zsh
$ readlink "/proc/$$/exe"
/bin/zsh
sample.sh
readlink "/proc/$$/exe"
$ bash sample.sh
/bin/bash
$ zsh sample.sh
/bin/zsh
シェル名のみ取得する場合には次のように実行します。
$ basename "$(readlink "/proc/$$/exe")"
zsh