# How to use exec and tee to redirect logs to stdout and a log file in the same time



## redriver (Oct 8, 2016)

I want to know how to do the same as I can do on Linux:
`exec &> >(tee -a "$log_file")`

The above command is found from http://unix.stackexchange.com/quest...ogs-to-stdout-and-a-log-file-in-the-same-time, but it does not work on FreeBSD 10.3


----------



## lme@ (Oct 8, 2016)

You probably need to run it in bash.


----------



## redriver (Oct 8, 2016)

It does not work either if I run it through bash script:

`honzhan@hz_BSD10:~/CodeTest/Shell % sh test1.sh
test1.sh: 2: Syntax error: redirection unexpected (expecting word)
honzhan@hz_BSD10:~/CodeTest/Shell % cat test1.sh
#!/bin/bash
exec &> >(tee -a log.txt)`


----------



## tobik@ (Oct 8, 2016)

redriver said:


> `honzhan@hz_BSD10:~/CodeTest/Shell % sh test1.sh`


sh(1) isn't bash on FreeBSD. The #! line is also wrong. On FreeBSD bash is installed as /usr/local/bin/bash. Not that it matters if you run the shell directly like you do.

Try again with `bash test1.sh`


----------



## Juha Nurmela (Oct 8, 2016)

Not your question, but in case you have to quickly unbashify something,

```
(
...lotsa commands...
) | tee log
```
does more or less the same

You'd expect `exec | command` would work like `exec > file`. Hmh...
Juha


----------

