title: How to capture the title of a terminal window in bash using ANSI escape sequences?
body: <p>I am using the bash command line in OSX. I know that the ANSI escape sequence \033[21t will retrieve the title of the current terminal window. So, for example:</p>
  
  <pre><code>$ echo -ne "\033[21t"
  ...sandbox... 
  $ # Where "sandbox" is the title of the current terminal window
  $ # and the ... are some extra control characters
  </code></pre>
  
  <p>What I'd like to do is capture this information programmatically in a script, but I can't figure out how to do it. What the script captures just the raw ANSI escape sequence. So, for further example, this little Ruby script:</p>
  
  <pre><code>cmd = 'echo -ne "\033[21t"'
  puts "Output from echo (directly to terminal):"
  system(cmd)
  terminal_name=`#{cmd}`
  puts "\nOutput from echo to variable:"
  puts terminal_name.inspect
  </code></pre>
  
  <p>Produces the following output:</p>
  
  <pre><code>Output from echo (directly to terminal):
  ^[]lsandbox^[\
  Output from echo to variable:
  "\e[21t"
  </code></pre>
  
  <p>I'd like the information in the second case to match the information shown on the terminal, but instead all I get is the raw command sequence. (I've tried using system() and capturing the output to a file -- that doesn't work, either.) Does anyone know a way to get this to work?</p>