In the first one, it looks to me that Bash goes in non-interactive mode if stderr is connected to a file when it starts. In that mode, it unsets PS1
, and hence doesn't print the prompt. It also shows in $-
, it doesn't contain the i
signifying interactive mode.
$ bash 2> file.txt
echo ${PS1-unset}
unset
echo $-
hBs
Well, the man page says it too:
An interactive shell is one started without non-option arguments (unless -s
is specified) and without the -c
option whose standard input and error are both connected to terminals (as determined by isatty(3)
), or one started with the -i
option. PS1
is set and $-
includes i
if bash is interactive, allowing a shell script or a startup file to test this state.
However, if you redirect stderr after the shell has started, you get the prompt in the file. As well as any input you write:
main$ PS1='\$ ' bash
$ exec 2> file.txt
hello
^D
main$ cat file.txt
$ echo hello
$ exit
In your second case, the inverse-colored ^G hints at the terminal bell control character, and that's used to end the escape sequences that set the terminal window title in e.g. xterm. Probably your prompt contains something like that, you can check with e.g. printf "%q\n" "$PS1"
to see the prompt with special characters encoded with backslashes.
Debian's /etc/bash.bashrc
contains this part with the title escape:
# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
# PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
...
It's in PROMPT_COMMAND
and not the prompt itself, but the idea is the same. The part between \033]0;
and \007
is what's set to the title.
{}
icon.