LoginSignup
1
1

More than 5 years have passed since last update.

Bash 5.0 で追加された変数の紹介

Last updated at Posted at 2019-01-09

Bash 5.0 がリリースされました

以下の変数が新機能として追加された。

  • BASH_ARGV0
  • EPOCHSECONDS
  • EPOCHREALTIME

Bash のアップグレード

macOS は brew でアップグレードできる

macOS ユーザーは homebrew で bash を管理していれば

brew update && brew upgrade

で Bash をアップグレードできます。

homebrew で bash を管理したいなら

brew install bash

とすればOK

ソースからビルドする

Bash-5.0 release available 👈 に bash-5.0.tar.gz のダウンロードリンクがある。これを展開した中の README にビルド方法が書かれている。macOS ユーザーもそれ以外 (Linux, Ubuntu とか) もソースからビルドして bash 5.0 を試せる。

wget ftp://ftp.cwru.edu/pub/bash/bash-5.0.tar.gz
# もしくは ftp://ftp.gnu.org/pub/gnu/bash/bash-5.0.tar.gz
tar xvf bash-5.0.tar.gz
cd bash-5.0
CC=cc ./configure
make
./bash

bash バージョンの確認

Bash のバージョンは bash --version で確認できる。

$ bash --version
GNU bash, version 5.0.0(1)-release (x86_64-apple-darwin18.2.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <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 をインタラクティブシェルとして実行中に echo $BASH_VERSION で確認できる。

$ echo $BASH_VERSION
5.0.0(1)-release

BASH_ARGV0

e. BASH_ARGV0: a new variable that expands to $0 and sets $0 on assignment.

BASH_ARGV0 には $0 が保持されており、また $0 を設定する時には BASH_ARGV0 を書き換えれば良いらしい。

script.bash を以下の内容で作成。

echo $BASH_VERSION
echo $0
echo $BASH_ARGV0
BASH_ARGV0=new_bash
echo $BASH_ARGV0
echo $0

bash script.bash で実行してみる。

$ bash script.bash
5.0.0(1)-release
script.bash
script.bash
new_bash
new_bash

どのように活用するかは私には分からない。

EPOCHSECONDS

b. There is an EPOCHSECONDS variable, which expands to the time in seconds since the Unix epoch.

Unix 時間を秒単位で参照できる。

$ echo $EPOCHSECONDS
1547022153

date +%s の代わりに使える。

$ echo $EPOCHSECONDS; date +%s
1547022153
1547022153

EPOCHREALTIME

c. There is an EPOCHREALTIME variable, which expands to the time in seconds since the Unix epoch with microsecond granularity.

Unix 時間を microsecond 単位で参照できる。

$ echo $EPOCHREALTIME
1547022153.595532
1
1
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
1
1