Fix a few minor bugs in the command-line shell

This is a rollup of a few fixes for some minor issues in the terminal shell. It includes a change to the encoding behaviour that (besides fixing a bug) might affect users in non-UTF-8 locales. See the commit messages for details.