Get the output of time(1) in bash
Clash Royale CLAN TAG#URR8PPP
Get the output of time(1) in bash
I am using a bash to compile c++ and Java programs.
I need to use the data that time adds, "real", "User", "Sys".
Even though I used var=$( compile ) to get output it shows nothing.
Is there a way to get as a varialbe the outout data of time?
Edited:
I am running the bash on Java with a Proccess and reading the InputStream with a BufferReader.
And closely related (asks about redirection to file, but underlying problem is the same): stackoverflow.com/questions/13356628/…
– Benjamin W.
Aug 7 at 19:27
and BashFAQ/032
– Benjamin W.
Aug 7 at 19:28
2 Answers
2
You can get the full output time echo "hi"
, and save this to a variable:
time echo "hi"
stats=$( time echo "hi"; 2>&1)
or to a file:
time echo "hi"; >mylogfile.txt 2>&1
The brace syntax here lets bash know what actual information you want. Because time is a reserved word in bash (like if
or while
), as well as a built-in, redirecting stdout
or stderr
without braces will not redirect the output from time
. To do that, we need to group all the output from the command together, and then redirect it.
if
while
stdout
stderr
time
However, if you only want the timing info, we can redirect the command's information to the void /dev/null
and keep only time
's output. We can save this to a variable:
/dev/null
time
stats=$( time echo "hi" 1>/dev/null 2>&1; 2>&1)
or to a file:
time echo "hi" 1>/dev/null 2>&1; 2>mylogfile.txt
time
is, at least in Bash, more likely a built-in, so man time
isn't authoritative, as it refers the binary at /usr/bin/time
or wherever it is installed; help time
or the Bash man page would have to be consulted.– Benjamin W.
Aug 7 at 19:58
time
man time
/usr/bin/time
help time
(Which has on effect on the solution of redirecting stderr.)
– Benjamin W.
Aug 7 at 19:59
help time
is super unhelpful imo. man time
at least points out that the output is in stderr, even if it shouldn't be taken as gospel.– jeremysprofile
Aug 7 at 20:01
help time
man time
Helpful or not,
man time
talks about something most likely not being executed when you do time <command>
. It's not shipped with Bash. man bash
has more information under SHELL GRAMMAR / Pipelines, but still doesn't mention that output goes to stderr. It could be argued that writing to stderr is simply POSIX conformant and also intuitively makes sense, as otherwise the output from time
would pollute a pipeline that's being measured.– Benjamin W.
Aug 7 at 20:10
man time
time <command>
man bash
time
Time output goes to the error output, so you can use getErrorStream in the process, and then you will get the output from time.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Cross-site dupe: serverfault.com/questions/175376/…
– Benjamin W.
Aug 7 at 19:26