#433: Set env vars right in the aliases
diff --git a/tests/test_shells.py b/tests/test_shells.py
index 060419f49..1cea2fb98 100644
--- a/tests/test_shells.py
+++ b/tests/test_shells.py
@@ -50,8 +50,8 @@ def test_app_alias(self, shell):
         assert 'alias fuck' in shell.app_alias('fuck')
         assert 'alias FUCK' in shell.app_alias('FUCK')
         assert 'thefuck' in shell.app_alias('fuck')
-        assert 'TF_ALIAS' in shell.app_alias('fuck')
-        assert 'PYTHONIOENCODING=utf-8' in shell.app_alias('fuck')
+        assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
+        assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
 
     def test_get_history(self, history_lines, shell):
         history_lines(['ls', 'rm'])
@@ -112,8 +112,8 @@ def test_app_alias(self, shell):
         assert 'alias fuck' in shell.app_alias('fuck')
         assert 'alias FUCK' in shell.app_alias('FUCK')
         assert 'thefuck' in shell.app_alias('fuck')
-        assert 'TF_ALIAS' in shell.app_alias('fuck')
-        assert 'PYTHONIOENCODING=utf-8' in shell.app_alias('fuck')
+        assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
+        assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
 
     def test_get_history(self, history_lines, shell):
         history_lines(['ls', 'rm'])
@@ -193,8 +193,8 @@ def test_app_alias(self, shell):
         assert 'function fuck' in shell.app_alias('fuck')
         assert 'function FUCK' in shell.app_alias('FUCK')
         assert 'thefuck' in shell.app_alias('fuck')
-        assert 'TF_ALIAS' in shell.app_alias('fuck')
-        assert 'PYTHONIOENCODING=utf-8' in shell.app_alias('fuck')
+        assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
+        assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
 
     def test_get_history(self, history_lines, shell):
         history_lines(['- cmd: ls', '  when: 1432613911',
@@ -252,8 +252,8 @@ def test_app_alias(self, shell):
         assert 'alias fuck' in shell.app_alias('fuck')
         assert 'alias FUCK' in shell.app_alias('FUCK')
         assert 'thefuck' in shell.app_alias('fuck')
-        assert 'TF_ALIAS' in shell.app_alias('fuck')
-        assert 'PYTHONIOENCODING=utf-8' in shell.app_alias('fuck')
+        assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck')
+        assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
 
     def test_get_history(self, history_lines, shell):
         history_lines([': 1432613911:0;ls', ': 1432613916:0;rm'])
diff --git a/thefuck/shells.py b/thefuck/shells.py
index f78d25755..9bd7fd3a3 100644
--- a/thefuck/shells.py
+++ b/thefuck/shells.py
@@ -38,8 +38,8 @@ def to_shell(self, command_script):
         return command_script
 
     def app_alias(self, fuck):
-        return "alias {0}='TF_ALIAS={0} PYTHONIOENCODING=utf-8 " \
-               "eval $(thefuck $(fc -ln -1))'".format(fuck)
+        return "alias {0}='eval $(TF_ALIAS={0} PYTHONIOENCODING=utf-8 " \
+               "thefuck $(fc -ln -1))'".format(fuck)
 
     def _get_history_file_name(self):
         return ''
@@ -103,8 +103,8 @@ def _script_from_history(self, line):
 
 class Bash(Generic):
     def app_alias(self, fuck):
-        return "TF_ALIAS={0} alias {0}='PYTHONIOENCODING=utf-8 " \
-               "eval $(thefuck $(fc -ln -1));" \
+        return "alias {0}='eval " \
+               "$(TF_ALIAS={0} PYTHONIOENCODING=utf-8 thefuck $(fc -ln -1));" \
                " history -r'".format(fuck)
 
     def _parse_alias(self, alias):
@@ -152,7 +152,7 @@ def app_alias(self, fuck):
                 '  set -l exit_code $status\n'
                 '  set -l fucked_up_command $history[1]\n'
                 '  env TF_ALIAS={0} PYTHONIOENCODING=utf-8'
-                '    thefuck $fucked_up_command | read -l unfucked_command\n'
+                ' thefuck $fucked_up_command | read -l unfucked_command\n'
                 '  if [ "$unfucked_command" != "" ]\n'
                 '    eval $unfucked_command\n'
                 '    if test $exit_code -ne 0\n'
@@ -203,9 +203,8 @@ def how_to_configure(self):
 
 class Zsh(Generic):
     def app_alias(self, fuck):
-        return "TF_ALIAS={0}" \
-               " alias {0}='PYTHONIOENCODING=utf-8 " \
-               "eval $(thefuck $(fc -ln -1 | tail -n 1));" \
+        return "alias {0}='eval $(TF_ALIAS={0} PYTHONIOENCODING=utf-8" \
+               " thefuck $(fc -ln -1 | tail -n 1));" \
                " fc -R'".format(fuck)
 
     def _parse_alias(self, alias):
diff --git a/thefuck/types.py b/thefuck/types.py
index 89c9d862f..f58646588 100644
--- a/thefuck/types.py
+++ b/thefuck/types.py
@@ -281,4 +281,6 @@ def run(self, old_cmd):
         if settings.alter_history:
             shells.put_to_history(self.script)
         # This depends on correct setting of PYTHONIOENCODING by the alias:
+        logs.debug(u'PYTHONIOENCODING: {}'.format(
+            os.environ.get('PYTHONIOENCODING', '>-not-set-<')))
         print(self.script)

Fix #433
