LoginSignup
8
3

More than 1 year has passed since last update.

Homebrewを使った Macのbashバージョン変更

Last updated at Posted at 2021-10-01

きっかけ

declareコマンドの-Aオプションが使えない、、、

declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

同じエラーで質問している人がいました
https://stackoverflow.com/questions/6047648/associative-arrays-error-declare-a-invalid-option

ふむふむ、bashのバージョンをあげてみよう

変更前のバージョンは3.2.57でした

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

Homebrewを使ってインストール

% brew install bash

インストール完了しましたが、このままだとdefaultで最新バージョンのbashが使えないようです

% which -a bash
/opt/homebrew/bin/bash
/bin/bash

/bin/bashが元々インストールされていた古いもので、
/opt/homebrew/bin/bashが先ほどインストールしたものです。

これをまず/usr/local/binに置いてみます
sudo cp /opt/homebrew/bin/bash /usr/local/bin/bash

bashのバージョンを見てみると、、

% which bash
/bin/bash
% bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin20)
Copyright (C) 2007 Free Software Foundation, Inc.

まだ古い方になっています

chshでdefaultで使用するbashを変えてみる

chsh -s /usr/local/bin/bash
Changing shell for username.
Password for username: 
chsh: /usr/local/bin/bash: non-standard shell

non-standard shellだよ〜と怒られてしまって変更ができません

/etc/shellsを編集

/etc/shellsを編集してみます

sudo vi /etc/shells

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

#/bin/bash #古いバージョンをコメントアウト
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash #新しいバージョンを追加

確認

chsh -s /usr/local/bin/bashを再度実行し、bashのバージョンを確認します

$ which bash
/usr/local/bin/bash
$ bash --version
GNU bash, バージョン 5.1.8(1)-release (aarch64-apple-darwin20.4.0)
Copyright (C) 2020 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.

無事新しいバージョンに切り替わりました

参考にした記事

https://itnext.io/upgrading-bash-on-macos-7138bd1066ba
https://blog.tottokug.com/entry/2017/12/31/121231

8
3
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
8
3