title: Remove color and redirect ouptut of a bash script from within
body: <p>My question is simple, how can I redirect all outputs of a bash script to <strong>file and terminal</strong> and <strong>remove color</strong> characters <strong>from within</strong> the script itself?</p>
  
  <p>I can't find an answer which fits all my needs.</p>
  
  <p>So far I tried <code>tee</code> to output to file and terminal, combined with <code>2&gt;&amp;1</code> to get <code>stederr</code> and <code>stdout</code>, <code>sed</code> to remove color characters and all of this with <code>exec</code> to do it from within my script but it doesn't work, I only get colored logs into the terminal and nothing in file.</p>
  
  <pre><code>#!/usr/bin/env bash
  exec 2&gt;&amp;1 | sed -r 's/\x1b\[[0-9;]*m//g' | tee script.logs
  
  python somepython.py
  python someotherpython.py
  </code></pre>
  
  <p>Here the python scripts produce outputs which are colored.</p>
  
  <p>I want to log those to terminal (untouched) and in file (without the color). In reality there is a lot more going on in my bash script than those just two python scripts, that's why I want to globally redirect the output of my script bash and not just pipe after each python script.</p>
  
  <p>Thus I used <code>exec</code> because I though it allowed to redirect all output produce by a script.</p>
  
  <p>Thanks in advance for any advice and help,</p>
  
  <p>PS: I dont want colored logs in file but I don't care in terminal if this is what needs to be done for logs to not be colored in file.</p>