How to check whether you've returned to shell
Compare PID of shell before and after running a command (same PID means same shell):
$ echo $$ 6215 $ bash --posix bash-4.3$ echo $$ 10230 bash-4.3$ exit $ echo $$ 6215
In the demo above, you can see me starting an application, in this case another bash in POSIX mode. Upon returning , we verify that the PID of the shell we use is the same, thus we can assume this is the actual shell. That of course can be automated by adding $$ to your shell prompt:
$ PS1="[$$] $PS1 " [6215] $
Variation on the theme can be done with checking a sha256sum ( or any other checksum , like md5) before and after. Sha-sums , if you don't know, are often used to check integrity of files, especially for downloads and iso images. In the demonstration below, we use /proc/<SHELL PID>/exe file, which is a symlink to actual binary (in this case , it would be my mksh shell). This way we can verify that the executable we're running is the same one.
[12107][xieerqi][21:34]: $ sha256sum /proc/$$/exe 70a16895186ddfac12343e816f05783cf60092c0980fc20c2ae4bc53b48f28e6 /proc/12107/exe [12107][xieerqi][21:34]: $ bash --posix bash-4.3$ sha256sum /proc/$$/exe c2615a71ff5c004e51aef248103a2950c25715f5eb8130837695770e1d78ecfa /proc/12434/exe bash-4.3$ exit [12107][xieerqi][21:35]: $ sha256sum /proc/$$/exe 70a16895186ddfac12343e816f05783cf60092c0980fc20c2ae4bc53b48f28e6 /proc/12107/exe
Return to shell isn't guarantee of safety
As for key-loggers, they don't necessarily need to be inside shell. If you're using graphical terminal emulator, a key-logger can simply listen to the keystrokes being sent to any window, regardless of shell you use.
As far as shells themselves, it's possible to launch a process in background. For instance, shell scripts use ampersand like so command & to launch something in background. I'm not sure if a background process can still read keys, but the fact that shell exits isn't a guarantee that the app exited. In the small demo below, you can see a function being launched in background, the script seemingly exits, and yet the function still writes to out.txt every second.
[10754][xieerqi][21:12]: $ cat launch_background_app.sh #!/bin/bash run_in_background() { while true; do date +%s > out.txt sleep 1 done } run_in_background & [10754][xieerqi][21:12]: $ ./launch_background_app.sh [10754][xieerqi][21:12]: $ cat out.txt 1484280777 [10754][xieerqi][21:12]: $ cat out.txt 1484280778 [10754][xieerqi][21:12]: $ cat out.txt 1484280779
NOTE TO EDITORS: please don't remove the prompt from my example -it is there for demonstration purposes to show that shell PID remains the same.
P.S: security starts with installing a trusted application in the first place, so consider ensuring the integrity of the application before using it in the first place.