DEV Community

kaede
kaede

Posted on • Edited on

Zsh 基礎 2 -- Ubuntu でログインシェルと tmux のデフォルトシェルを zsh に変更する

why

ターミナルの現在地とコマンドを打つところを分離したかった

そのためには zsh にしてテーマを適用する必要があった

新しい Mac OS の場合は最初から Zsh になっている。
なので変更の必要はない。


現在のシェルが bash なのを確認

https://hirooooo-lab.com/development/change-mac-shell/

hirooooo-lab の記事を参考に変更を試みる

kaede@ship:~$ echo $SHELL 
Enter fullscreen mode Exit fullscreen mode

/bin/bash

$SHELL の変数をみると、現在のシェルがわかる。


入っているシェルリストに zsh がないのを確認

/etc/shells の中身を見ると

# /etc/shells: valid login shells /bin/sh /bin/bash /usr/bin/bash /bin/rbash /usr/bin/rbash /bin/dash /usr/bin/dash /usr/bin/tmux 
Enter fullscreen mode Exit fullscreen mode

dash や tmux はあるが、zsh がなかった。

which zsh で探しても出力されない。


zsh のインストール

https://qiita.com/sayama0402/items/ac9e078b024c3287f99f

sudo apt-get install zsh The following additional packages will be installed: zsh-common Suggested packages: zsh-doc The following NEW packages will be installed: zsh zsh-common Unpacking zsh-common (5.8-3ubuntu1.1) ... Unpacking zsh (5.8-3ubuntu1.1) ... Setting up zsh-common (5.8-3ubuntu1.1) ... Setting up zsh (5.8-3ubuntu1.1) ... Processing triggers for man-db (2.9.1-1) ... 
Enter fullscreen mode Exit fullscreen mode

zsh と zsh-common がインストールされた。


zsh のセットアップ

zsh

このコマンドで bash から zsh を起動できる。

This is the Z Shell configuration function for new users, zsh-newuser-install. You are seeing this message because you have no zsh startup files (the files .zshenv, .zprofile, .zshrc, .zlogin in the directory ~). This function can help you with a few settings that should make your use of the shell easier. 
Enter fullscreen mode Exit fullscreen mode
You can: (q) Quit and do nothing. The function will be run again next time. (0) Exit, creating the file ~/.zshrc containing just a comment. That will prevent this function being run again. (1) Continue to the main menu. (2) Populate your ~/.zshrc with the configuration recommended by the system administrator and exit (you will need to edit the file by hand, if so desired). --- Type one of the keys in parentheses --- 
Enter fullscreen mode Exit fullscreen mode

最初の Z Shell の設定のガイドがでてきた。
Quit か Exit か メインメニューに行くか、管理者のおすすめにするかを選べる。

管理者のおすすめにしてみる。

2

/home/kaede/.zshrc:15: scalar parameter HISTFILE created globally in function zsh-newuser-install kaede@machine ~ % 
Enter fullscreen mode Exit fullscreen mode

zsh の一時的な起動

zsh と打てば bash から zsh が開く。

Image description

ユーザー名と端末名が水色背景
現在のディレクトリが水色文字

に最初からなっていて、bash よりこの時点でみやすい。

Image description

現在いるパスが長くなると、
自動でコマンドを打つところは改行してくれるらしい。便利。

この % の位置を > マークにしたいので拡張をいれたい。
その前にログイン時に zsh が開くようにする。


ログインシェルのデフォルトを zsh にする

Image description

新しく zsh を入れただけだと、exit して再び新しいシェルを開くと
当然 bash が開く。

https://qiita.com/sayama0402/items/ac9e078b024c3287f99f#%E8%BF%BD%E8%A8%98

chsh, CHange SHell でデフォルトで開くシェルを変えられるようだ

chsh -s $(which zsh) 
Enter fullscreen mode Exit fullscreen mode

Image description

ターミナルアプリを再起動すると zsh が開いた


$SHELL 変数が bash のままなのを確認

echo $SHELL 
Enter fullscreen mode Exit fullscreen mode

/bin/bash

しかし、$SHELL 変数の値は bash のままだ。

新しいタブも zsh で開くが、この変数を変えないと
tmux は zsh で開かない。


SHELL 変数を zsh にして tmux も zsh で開くようにする

$SHELL に応じて開くのか、
tmux を開くと zsh ではなく bash で開いてしまう。

https://unix.stackexchange.com/a/430460

この回答を読むと、chsh は $SHELL を書き換えない。
なので /etc/passwd を書き換えろと書いてあった。

root:x:0:0:root:/root:/bin/bash 
Enter fullscreen mode Exit fullscreen mode

ただちょっとこれを書き換えるのは怖いので
.zshrc でエクスポートして変えるようにする

# change SHELL value export SHELL="/usr/bin/zsh" 
Enter fullscreen mode Exit fullscreen mode

SELL に zsh のパスを入れて export する。

~ % echo $SHELL /usr/bin/zsh 
Enter fullscreen mode Exit fullscreen mode

入っている!

Image description

これで tmux 起動後に zsh に入れた!

P.S

Image description

https://mikebuss.com/2014/02/02/a-beautiful-productive-terminal-experience/

zsh を使うと、コマンドを typo した時に
正しい候補を探してくれて
y を押すと、直して再実行もしてくれる。


まとめ

sudo apt-get install zsh で zsh をインストールできる

chsh -s $(which zsh) でログインシェルを zsh に変更できる

export SHELL="/usr/bin/zsh" を .bashrc に書くことで
tmux のログインシェルも zsh に変更できる。

Top comments (1)

Collapse
 
sso profile image
Sall

🥷 🧙‍‍♀️ wiki.zshell.dev 🧙‍‍♀️ 🥷