macOS 10.15.2 へ bash.5 系をインストールし補完出来るようにする

macOS 10.15.2 (Catalina) に bash 5.x をインストールする手順をメモしておきます。

bash 5.x のインストール

私の環境では bash 3.2.57 がインストールされていました。

% bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Copyright (C) 2007 Free Software Foundation, Inc.

Homebrew に bash 5.0.11 がありました。

% brew info bash
bash: stable 5.0.11 (bottled), HEAD
Bourne-Again SHell, a UNIX command interpreter
Bash - GNU Project - Free Software Foundation
Not installed From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/bash.rb ==> Options --HEAD Install HEAD version ==> Analytics install: 18,398 (30 days), 62,368 (90 days), 316,823 (365 days) install-on-request: 15,364 (30 days), 51,766 (90 days), 255,867 (365 days) build-error: 0 (30 days)

インストールします。

brew install bash

シェルの変更が出来るように、「変更可能なシェル一覧」(/etc/shells) へ brew で追加インストールした bash 5.x を追加します。

sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'

chsh でログインシェルを変更します。 chsh を実行するとログイン中ユーザのパスワードを入力するよう促されますので、パスワードを入力してシェルの変更を完了します。

chsh -s /usr/local/bin/bash

これで新たにシェルを起動すると bash 5.0.11 が起動するようになりました。

$ bash --version
GNU bash, バージョン 5.0.11(1)-release (x86_64-apple-darwin19.0.0)
Copyright (C) 2019 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 またはそれ以降 <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

補完出来るようにする

bash で補完が出来るよう、パッケージを追加インストールします。 Homebrew には補完用パッケージとして bash 3.2 と互換のある bash-completion と bash 4.1 以上と互換のある bash-completion@2 があるようです。

% brew info bash-completion
bash-completion: stable 1.3 (bottled)
Programmable completion for Bash 3.2
Debian / bash-completion · GitLab
Debian packaging for bash-completion
Conflicts with: bash-completion@2 (because Differing version of same formula) Not installed From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/bash-completion.rb ==> Caveats Add the following line to your ~/.bash_profile: [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" ==> Analytics install: 14,157 (30 days), 43,678 (90 days), 183,138 (365 days) install-on-request: 13,167 (30 days), 40,548 (90 days), 167,932 (365 days) build-error: 0 (30 days)
% brew info bash-completion@2
bash-completion@2: stable 2.10 (bottled), HEAD
Programmable completion for Bash 4.1+
GitHub - scop/bash-completion: Programmable completion functions for bash
Programmable completion functions for bash. Contribute to scop/bash-completion development by creating an account on GitHub.
Conflicts with: bash-completion (because Differing version of same formula) Not installed From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/bash-completion@2.rb ==> Dependencies Required: bash ✘ ==> Options --HEAD Install HEAD version ==> Caveats Add the following to your ~/.bash_profile: [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" If you'd like to use existing homebrew v1 completions, add the following before the previous line: export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d" ==> Analytics install: 4,328 (30 days), 8,812 (90 days), 28,014 (365 days) install-on-request: 4,239 (30 days), 8,703 (90 days), 27,776 (365 days) build-error: 0 (30 days)

今回は bash 5.x を使っている為、bash-completion@2 をインストールします。

brew install bash-completion@2

インストールが完了したら、~/.bash_profile へ必要な設定を実施します。

echo '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >> ~/.bash_profile

これで bash 5.x でも補完が効くようになりました。

コメント

タイトルとURLをコピーしました