22
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

年月日時分秒の値を個別のシェル変数に一発で入れるやつ

Last updated at Posted at 2016-04-21

シェルスクリプトで年/月/日/時/分/秒とかの値を個別の変数に入れたい時って今まで↓こんな風にしてたんだけど。

# 何回もdate実行するとその間に日付変わる可能性あるので%sの値を使い回す
s=$(date +%s)
Y=$(date -d @$s +%Y)
m=$(date -d @$s +%m)
d=$(date -d @$s +%d)
:

↓これでよかったw

read s Y m d H M S ms ns <<< "$(date +'%s %Y %m %d %H %M %S %3N %9N')"

date の実行が1回で済むから無駄もないし、変数間での時間のズレも気にしなくていいし良いこと尽くめじゃんw 何で今まで思いつかなかったんだろう。

22
21
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
22
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?