Search code examples
macosshelltimegnu

Time command's illegal option


I am having some issues with the time command time. It works fine but since time gives 3 numbers in output(real, user and sys) I was looking on how to get only the user time. I found many posts that say to use /usr/bin/time -f "%U" but I get this errors :

/usr/bin/time: illegal option -- f
usage: time [-lp] command.

I also can't find the option -f when I type man time

I tried to skip it and write:/usr/bin/time "%U" and I get %U: No such file or directory

I have installed GNU-time but nothing changed. what is the issue?


Solution

  • There are several time commands...


    If you run:

    type time
    

    and get:

    time is a shell keyword
    

    that means you are using bash's built-in shell command that does not accept the option you want. The help for this command, like all shell built-ins, is available using:

    help time
    

    If you run:

    type time
    

    and get:

    time is hashed (/usr/bin/time)
    

    that means you are using the Apple-supplied (BSD-like) time command that does not accept the option you want. That is also the one described in the man-pages.


    If you installed gnu-time with homebrew, you need to run:

    gtime
    

    unless you didn't set your PATH to include /usr/local/bin like you should if you use homebrew, in which case you would need to run:

    /usr/local/bin/gtime
    

    and view the man-page with:

    man gtime
    

    If you want to always use the command time in place of gtime, and I would not recommend this, you need to run:

    brew info gnu-time
    

    and read what it says about setting:

    PATH="/usr/local/opt/gnu-time/libexec/gnubin:$PATH"