title: How to strip ANSI escape sequences from a variable?
body: <p>Weird question. When I set a variable in Bash to display as a certain color, I don't know how to reset it. Here is an example:</p>
  
  <p>First define the color code:</p>
  
  <pre><code>YELLOW=$(tput setaf 3)
  RESET=$(tput sgr0)    
  </code></pre>
  
  <p>Now set the error message variable and color part of it.</p>
  
  <pre><code>ERROR="File not found: "$YELLOW"Length.db$RESET"
  </code></pre>
  
  <p>This sets the variable ERROR as the error message to be returned from a function that will eventually be displayed on the terminal. The error will be all white with the exception of the file name. The file name is highlighted yellow for the user.</p>
  
  <p>This works great except when logging with rsyslog. When the error message gets logged, it comes out something like this:</p>
  
  <p>File not found: #033[33mLength.db#033(B#033[m</p>
  
  <p>This obviously makes log files very difficult to read. At first I figured I could process using sed the error message immediately after outputting to the terminal but before logging, but there is nothing to search for and replace. ie, I thought I could use sed to do something similar to this:</p>
  
  <pre><code>ERROR=$(echo "$ERROR" | sed -r 's%\#033\[33m%%')
  </code></pre>
  
  <p>But those characters are not present when you echo the variable (which makes sense since you dont see it on the terminal). So im stuck. I dont know how to reset the color of the variable after setting it. I also tried to reverse the process somehow using $RESET but maybe my syntax is wrong or something.</p>