17
14

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.

Bashで「標準出力」「エラー出力」「実行時間」を全て変数に代入する方法

Last updated at Posted at 2016-06-11

Bashのプロセス置換を使っています.

なお,Zshでは何故か動きません
(timeのフォーマットの違いの問題では無く2>で流れない)

# !/bin/bash

action() {
    sleep 1
    echo 'stdout'
    >&2 echo 'stderr'
}

unset stdout stderr realtime
eval "$(
    { time action >&3 2>&4; } \
    2> >(realtime=$(awk '$1=="real" {print $2}'); typeset -p realtime) \
    3> >(stdout=$(cat); typeset -p stdout) \
    4> >(stderr=$(cat); typeset -p stderr)
)"

echo "stdout: $stdout"
echo "stderr: $stderr"
echo "realtime: $realtime"
17
14
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
17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?