shell bash add tag
PeterVandivier
Given `foo.sh` looks like this...

```bash
#!/bin/bash

echo good
echo bad 1>&2
```

... I can see how long it takes to run `foo.sh` by prefixing the [`time`](https://linux.die.net/man/1/time) utility.

```bash
time ./foo.sh

good
bad

real    0m0.003s
user    0m0.002s
sys     0m0.001s
```

I would like capture the output of `time` and log it to a file. Sadly neither redirection or capture appear to work as I expect.

![Screenshot 2020-09-23 at 08.58.15.png](/image?hash=8a6301b151e6efa50ff665b69a2310005f3d68adc630e8d539f1192908bb7c51)

Is there a naïve method to capture and log this output?

---

^n.b.^ ^Wrapping^ ^the^ ^command^ ^appears^ ^also^ ^to^ ^not^ ^work^ [^(screenshot)^](/image?hash=f1911dcade3262ca6df549e8e4f064ed9fe7fe936f1f3b3b62457fa024f9eff3)

^n.b.2^ ^coreutils^ ^`--output`^ ^arg^ ^appears^ ^to^ ^be^ ^screwing^ ^up^ ^for^ ^me^ ^as^ ^well^ [^(screenshot)^](/image?hash=018e88854b8e161d5e6b03ab850c4d9be040e48cdd20263b687ce045e82fe98a)
Top Answer
Jack Douglas
As you imply, the [`time` command](https://linux.die.net/man/1/time) outputs the timing info to stderr:

> time writes a message to standard error giving timing statistics about this program run

However your redirection is happening to the output of `./foo.sh`, not the output of `time`.

To force that you can use braces:

```
# { time ./foo.sh; } &> foo.log 
# cat foo.log 
good
bad

real	0m0.005s
user	0m0.001s
sys	0m0.005s
```

---

*Side note*: you are using the Bash built-in version of `time`:

::: tio S0oszvj/v6SyIFWhJDM39f9/AA
§§§ bash bash
type time
§§§
``` none
time is a shell keyword
```
:::

There is also [a GNU version](https://www.gnu.org/software/time/) with various options and a different default output format.

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.