たけまるの日記

たけまるの日記です。web関係の技術ネタが多いですが、好きなことを適当に書いています。

macで最新のcoreutlsを使う方法

coreutilsUNIX系OSの基本的なコマンド(lsとかcpとか)を提供するパッケージである。

今回はmaccoreutilsに含まれるsortコマンドの-Rオプションを使いたかったのだが、macosが標準で提供するsortコマンドが古いバージョンのため使用できなかった。 このため、homebrewからcoreutilsをインストールし、使用する方法を実施してみた。

ちなみにsortコマンドの-Rオプションはランダムでソートするオプションであり、バージョン6以降で利用できる。

実施前

$ sort --version
sort (GNU coreutils) 5.93

homebrewによりcoreutilsをインストール

$ brew install coreutils 
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/coreutils-8
######################################################################## 100.0%
==> Pouring coreutils-8.23.mavericks.bottle.tar.gz
==> Caveats
All commands have been installed with the prefix 'g'.

If you really need to use these commands with their normal names, you
can add a "gnubin" directory to your PATH from your bashrc like:

    PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

Additionally, you can access their man pages with normal names if you add
the "gnuman" directory to your MANPATH from your bashrc as well:

    MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

==> Summary
🍺  /usr/local/Cellar/coreutils/8.23: 214 files, 10M

PATHの順序次第では標準のcoreutilsをすべてbrew版に置き換えることもできるが、今回はsortコマンドだけ使えればよかったので

# ~/.bashrc 
alias sort='/usr/local/opt/coreutils/libexec/gnubin/sort'

で逃げることにした。

$ sort --version
sort (GNU coreutils) 8.23

$ echo -e "a\nb\nc" | sort -R
c
b
a

無事、sortコマンドの-Rオプションが使えるようになった。

参考