Fix that REPL crashes in shell mode on $a<tab> and added a test.
Merge pull request #9055 from dhoegh/shell_completion_fix_back_port

Backport of #8995 to 0.3