LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   time command in bash is aliased or defined... but how? (https://www.linuxquestions.org/questions/linux-general-1/time-command-in-bash-is-aliased-or-defined-but-how-4175520140/)

Beryllos 09-26-2014 01:09 AM

time command in bash is aliased or defined... but how?
 
I'm getting some strange behavior of the time command. It ignores command options. It must have been redefined, but I can't find an alias or function for it, so I don't know how it has been redefined. Here is what I am seeing:

Normal time command (no options):
Code:

$ time sleep 0.1

real        0m0.103s
user        0m0.000s
sys        0m0.000s
$

time command with --format option, then --help option:
Code:

$ time --format="%e" sleep 0.1
bash: --format=%e: command not found

real        0m0.002s
user        0m0.000s
sys        0m0.000s
$ time --help
bash: --help: command not found

real        0m0.002s
user        0m0.000s
sys        0m0.000s
$

As you see, it is expecting nothing but a command following the word "time." When I provide command options, they are simply passed on to the bash command interpreter, which gives the error messages shown above.

If I give the full path of the executable, it works fine:
Code:

$ /usr/bin/time sleep 0.1
0.00user 0.00system 0:00.10elapsed 0%CPU (0avgtext+0avgdata 560maxresident)k
0inputs+0outputs (0major+185minor)pagefaults 0swaps
$ /usr/bin/time --format="%e" sleep 0.1
0.10
$ /usr/bin/time --help
Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose]
      [--portability] [--format=format] [--output=file] [--version]
      [--quiet] [--help] command [arg...]
$

With the full path specified, it works exactly as described in the man pages.

Diagnostics:
I am running Debian 7.5 (Wheezy). I checked .bashrc and .profile in my home directory. I checked /etc/bash.bashrc, /etc/profile, and files in the /etc/profile.d directory. I checked env. I checked alias. I tried which and whereis. I checked $PATH. I tried unset -f time.
Code:

$ uname -a
Linux Mister_Chips 3.2.0-4-686-pae #1 SMP Debian 3.2.57-3+deb7u2 i686 GNU/Linux
$ cat /etc/debian_version
7.5
$ grep time .bashrc
$ grep time .profile
$ grep time /etc/bash.bashrc
                # check because c-n-f could've been removed in the meantime
$ grep time /etc/profile
$ find /etc/profile.d -type f -exec grep time {} \;
$ env | grep time
$ alias
alias ls='ls --color=auto'
$ which time
/usr/bin/time
$ whereis time
time: /usr/bin/time /usr/bin/X11/time /usr/include/time.h /usr/share/man/man1/time.1.gz /usr/share/man/man7/time.7.gz /usr/share/man/man2/time.2.gz
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
$ for dir in /usr/local/bin /usr/bin /bin /usr/local/games /usr/games; do find $dir -name time; done
/usr/bin/time
$ unset -f time
$ time --help  # doing this to see if unset deleted the function...
bash: --help: command not found

real        0m0.002s
user        0m0.000s
sys        0m0.000s
$

In short, I cannot figure out why the time command is going wrong.

Help!

Also I am wondering how I can tell what other bash commands might have been redefined so that they do not work as described in the manual.

pan64 09-26-2014 01:12 AM

there are two different time commands available. One is the shell's built-in and the second one is /usr/bin/time. Do not mix them!

Beryllos 09-26-2014 01:29 AM

Quote:

Originally Posted by pan64 (Post 5244540)
there are two different time commands available. One is the shell's built-in and the second one is /usr/bin/time. Do not mix them!

Thanks.

Okay, I've just read man bash-builtins and don't see a built-in function by that name...

... but I do see in man bash that time is a reserved word:
Code:

RESERVED WORDS
      Reserved words are words that have a special meaning to the shell.  The
      following words are recognized as reserved when unquoted and either the
      first  word  of a simple command (see SHELL GRAMMAR below) or the third
      word of a case or for command:

      ! case  do done elif else esac fi for function if in select then  until
      while { } time [[ ]]

Also, now that I know what to look for, I see this advice in the later sections of man time:
Code:

      Users of the bash shell need to use an explicit path in order to run
      the external time command and not the shell builtin variant.  On system
      where time is installed in /usr/bin, the first example would become
            /usr/bin/time wc /etc/hosts

Okay, problem solved! That was fast. Thank you, pan64.

pan64 09-26-2014 01:37 AM

you are welcome
just use:
type time
which time

Beryllos 09-26-2014 01:40 AM

Quote:

Originally Posted by pan64 (Post 5244552)
you are welcome
just use:
type time
which time

Code:

$ type time
time is a shell keyword
$ which time
/usr/bin/time
$

I think I see.


All times are GMT -5. The time now is 12:35 PM.