Save in a variable the number of seconds a process took to run

后端 未结 5 674
走了就别回头了
走了就别回头了 2021-02-04 17:15

I want to run a process in bash and save in an env variable the number of seconds it took to run. How would I do such a thing?

5条回答
  •  醉话见心
    2021-02-04 17:46

    This works in Bash, and also Zsh:

    # Set time format to seconds
    TIMEFORMAT=%R
    # Time a process
    PROC_TIME=$(time (insert command here >/dev/null 2>&1) 2>&1)
    echo $PROC_TIME
    
    • The first two redirections hide your process's output ">/dev/null 2>&1"
    • The last redirect is needed because "time" prints the time on stderr

提交回复
热议问题