<p>I'm looking for a way to redirect all the stderr streams in interactive bash (ideally to its calling parent process).</p>

<p>I don't want to redirect stderr stream from each individual command, which I could do by appending <code>2&gt; a_file</code> to each command.</p>

<p>By default, these stderr streams are redirected to the stdout of an interactive bash. I would like to get them on the stderr of this interactive bash process in order to prevent my stdout to be polluted by error messages and be able to treat them separatly.</p>

<p>Any ideas?</p>

<p>I still haven't found an answer ... But maybe it's actually a tty parameter. Does anybody knows something about tty/interactive shell responsibility for handling stderr ?</p>

[{'answer_id': 30085, 'author': 'caerwyn', 'author_id': 2406, 'author_profile': 'https://Stackoverflow.com/users/2406', 'pm_score': 2, 'selected': False, 'text': '<p>You could launch a new bash process redirecting the stderr of that process:</p>\n\n<pre><code>  $ bash -i 2&gt; stderr.log\n  $ \n</code></pre>\n'}, {'answer_id': 30088, 'author': 'gavrie', 'author_id': 2794, 'author_profile': 'https://Stackoverflow.com/users/2794', 'pm_score': 3, 'selected': False, 'text': '<p>Use the <code>exec</code> builtin in bash:</p>\n\n<p><code>exec 2&gt; /tmp/myfile</code></p>\n'}, {'answer_id': 75132, 'author': 'jtimberman', 'author_id': 7672, 'author_profile': 'https://Stackoverflow.com/users/7672', 'pm_score': 2, 'selected': False, 'text': '<p>Try your commands in doublequotes, like so:</p>\n\n<pre><code>ssh remotehost "command" 2&gt;~/stderr\n</code></pre>\n\n<p>Tested on my local system using a nonexistant file on the remote host.</p>\n\n<pre><code>$ ssh remotehost "tail x;head x" 2&gt;~/stderr\n$ cat stderr \ntail: cannot open `x\' for reading: No such file or directory\nhead: cannot open `x\' for reading: No such file or directory\n</code></pre>\n'}, {'answer_id': 84426, 'author': 'Gunstick', 'author_id': 15653, 'author_profile': 'https://Stackoverflow.com/users/15653', 'pm_score': 2, 'selected': False, 'text': "<p>I don't see your problem it works as designed:</p>\n\n<pre><code>$ ssh remotehost 'ls nosuchfile; ls /etc/passwd' &gt;/tmp/stdout 2&gt;/tmp/stderr \n$ cat /tmp/stdout  \n/etc/passwd \n$ cat /tmp/stderr \nnosuchfile not found\n</code></pre>\n"}, {'answer_id': 731499, 'author': 'crb', 'author_id': 51691, 'author_profile': 'https://Stackoverflow.com/users/51691', 'pm_score': 0, 'selected': False, 'text': '<p>Tried <code>ssh -t</code> to create a pseudo-TTY at the remote end?</p>\n'}, {'answer_id': 4157470, 'author': 'Kit Gerrits', 'author_id': 504851, 'author_profile': 'https://Stackoverflow.com/users/504851', 'pm_score': 2, 'selected': False, 'text': '<p>Two things:</p>\n\n<ol>\n<li>Using <code>2&gt;&amp;1</code> in a remote ssh command results in the error ending up inside the local tarfile, resulting in a \'broken\' backup.</li>\n<li>If you want to apply a redirect on the other side of the ssh, remember to escape the redirect command.</li>\n</ol>\n\n<p>My suggestion would be to redirect <code>stderr</code> on the remote side to a file and pick it up later, in case of an error.</p>\n\n<p>example:</p>\n\n<pre><code>ssh -t remotehost tar -cf - /mnt/backup 2\\&gt;backup.err &gt; localbackup.tar\nEXITSTATUS=$?\nif [ $EXITSTATUS != "0" ] then \n  echo Error occurred!\n  ssh remotehost cat backup.err &gt;localbackup.errors\n  cat localbackup.errors\n  ssh remotehost rm backup.err \nelse \n  echo Backup completed successfully!\n  ssh remotehost rm backup.err \nfi\n</code></pre>\n'}, {'answer_id': 18093445, 'author': 'stefano', 'author_id': 1505651, 'author_profile': 'https://Stackoverflow.com/users/1505651', 'pm_score': 2, 'selected': False, 'text': "<p>I find a good way is to surround the commands by parentheses, '()', (launch a sub-shell) or curly-braces, '{}' (no sub-shell; faster):</p>\n\n<pre><code>{\n  cmd1\n  cmd2\n  ...\n  cmdN\n} 2&gt; error.log\n</code></pre>\n\n<p>Of course, this can be done on 1 line:</p>\n\n<pre><code>{ cmd1; cmd2; ... cmdN; } 2&gt; error.log\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/30066', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1044/']