1

I just found, that the following doesn't work on my Ubuntu 16.04 the way it should:

$ time >file 2>&1

real    0m0.000s
user    0m0.000s
sys     0m0.000s
$ cat file
$

Output goes to terminal, the file is empty, though with 2>&1 (descriptor 2 redirect to address of descriptor 1) STDERR is supposed to go where STDOUT goes. I've been looking for existing topics on this subject, and in description of a more complicated problem such a simple one as here is usually mentioned that it does well. Why could it be misbehaving like this?

UPD: got the answer, time is a built-in command in BASH. Maybe someone puzzles over exact same thing (likely those who learn Unix with old book of Kernighan and Pike).

codeforester
  • 39,467
  • 16
  • 112
  • 140

1 Answers1

1

You're using bash, which indeed has a built-in keyword called time which functions in a similar way.

Instead of using the built-in time (bash) command, you can access the installed binary (if any) like /usr/bin/time -p [command] >file 2>&1. Which will exactly do what you expect. Please note the -p option, which is needed for the "portable output format" you've seen above.

Also please take a look at the manpage: man time.

Michael Hirschler
  • 2,345
  • 16
  • 28
  • thank you, first time hear about built-in commands. I should have tried to code something in C that writes to STDERR and try it before asking) – Polazhinets.A Jun 30 '18 at 15:14
  • Glad to hear! Please keep in mind to accept the answer, so others can see that it's solved or helpful. – Michael Hirschler Jun 30 '18 at 15:18
  • If you ever need some stderr for testing in an interactive shell command, you can use `echo hello >&2` or just `cp` without arguments since it prints an error – that other guy Jun 30 '18 at 15:32