Search code examples
linuxbashtimebin

Need functionality from bash time command to behave


What I have now:

#!/bin/bash

/usr/bin/time -f "%E" rows=$(mysql -uuser -pXXXXXXXXXX --skip-column-names -e "use db; update users set create_time=now(); select row_count();") 
echo $rows

However I get this error when running:

/usr/bin/time: cannot run rows=12: No such file or directory
0:00.00 real

I need to use this real /usr/bin/time command and not the stand alone time command that is available. Is there any way I can use this command to get the functionality I want?


Solution

  • Variable assignment is a shell construct, you can't "run" it as a command, and if you could, it wouldn't have any effect on your current shell session. But luckily all you need to do is reorganize the command:

    rows=$(/usr/bin/time -f "%E" mysql -uuser -pXXXXXXXXXX --skip-column-names -e "use db; update users set create_time=now(); select row_count();")
    

    Since time displays the timing information to stderr by default, it will go to the console, and not be captured by the $() construct. mysql's output will be passed through and captured as usual.