Search code examples
linuxbashstdoutstderr

redirect stderr stdout both on console and log file


I am using a C API in order to manage my bluetooth thru bluetoothctl. It works by using command like :

./BT_API connect $2 | bluetoothctl > /tmp/BT_TMP

All is stored within /tmp/BT_TMP but noting on screen. I tried by using the following command

./BT_API connect $2 | bluetoothctl 2>&1 /tmp/BT_TMP

But now all is displayed on screen but file /tmp/BT_TMP is not created.


Solution

  • use tee, which will redirect the stdin to both a file and stdout:

    ./BT_API connect $2 | bluetoothctl 2>&1 | tee /tmp/BT_TMP