1

With bash, how do I store the time it takes for my program to execute in one variable, and the output of my program in another variable? I know that time sends its output to stdout; the closest I've got is this:

exec 3>&1 4>&2
time_output=$( { time echo hello world 1>&3 2>&4; } 2>&1 )

but "hello world" is printed to the terminal. How to I capture "hello world" to another variable?

I've also tried:

prog_output=$(time_output=$( { time echo hello world 1>&3 2>&4; } 2>&1 ) )

but this doesn't work. prog_output contains nothing and "hello world" is printed to the terminal.

2

1 Answer 1

1

Would this work for you?

prog_output=`( time ls ) 2> time_output`
time_output=`cat time_output`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.