古すぎるmacOSのbash… Ubuntu相当の5.0に更新しました

bashのcompleteコマンドオプション仕様がUbuntu 18.04のそれと異なるので、Pythonのargcompleteモジュールを使って生成したシェルスクリプトがエラー吐いて使えない…。

macOSのbashはバージョン3 (2019/05時点の最新は5)

なぜcompleteコマンドオプション仕様が異なるかというと、macOSのbashバージョンが3.x系に対して、Ubuntu等の新しめの環境では4.x以上だから。

macOSのbashが3.xを維持しているのは下記理由から。

The reason that Apple includes such an old version of Bash in its operating system has to do with licensing. Since version 4.0 (successor of 3.2), Bash uses the GNU General Public License v3 (GPLv3), which Apple does not (want to) support. You can find some discussions about this here and here. Version 3.2 of GNU Bash is the last version with GPLv2, which Apple accepts, and so it sticks with it.

some discussions about thisのリンク先を見ると、アップルがGPLv3を受け入れない理由はこれっぽいですね。そういえば前々職で似たような話が職場で問題になったな…。

GPL v3. require the code to be modifiable. So users would have to be able to make changes and run it. Much of Apple code is signed, so Apple would have to allow users to sign or run unsigned code. Very problematic on iOS for Apple.

Also has automatic patent licensing and Apple is very protective of its patents.

macOSのbashを更新する

更新手順は、上記のサイトを見れば全部解説されているのですが、日本語意訳として参考にしてください。

ざっくり以下3ステップの手順になります。

  1. 最新バージョンのBashをインストールする
  2. 最新バージョンのBashをログインシェルとして使用可能にする
  3. 最新バージョンのBashをデフォルトシェルに設定する

最新バージョンのBashをインストールする

Homebrew一発です。

$ brew install bash

上記コマンドでインストールすると、その後必要な手順を解説したログが出力されますが、とっておくのを忘れた…。
無事インストールが完了すると、/usr/local/bin下に最新バージョンのbashが入ります。

$ which -a bash
/usr/local/bin/bash
/bin/bash
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.
$ /usr/local/bin/bash --version
GNU bash, バージョン 5.0.7(1)-release (x86_64-apple-darwin17.7.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を/etc/shellsに追記します。

$ sudo vim /etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash ← 追記

最新バージョンのBashをデフォルトシェルに設定する

chshコマンドを実行して、デフォルトシェルを最新bashに変更します。

CHPASS(1) BSD General Commands Manual CHPASS(1)
NAME
chpass, chfn, chsh -- add or change user database information
SYNOPSIS
chpass [-l location] [-u authname] [-s newshell] [user]

実際にやってみるとこんな感じ。

$ sudo chsh -s /usr/local/bin/bash

セキュリティ上の理由から、/bin/bashを直接シンボリックリンク等で上書きできないらしい。

注意事項

シェルスクリプトを書く際には、シェバンの指定に注意すること。常に

#!/usr/bin/env bash

と書く癖がついてる人は問題ないけど、普段

#!/bin/bash

と書いている人は、良い機会なので改めましょう。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

Close Bitnami banner
Bitnami