1

I redirected standard error of bash command to a file and bash prompt got redirected. But when i print the content of file, it was empty. Where did the bash prompt go?

image

and again when i redirect stdout of bash to a file, it redirected the output and and not the prompt as expected but while printing the content of file, there were some characters form the prompt too. How?

enter image description here

value of $PS1 and $PROMPT_COMMAND:

enter image description here

please explain this to me.

1
  • Please don't post images of text. Copy and paste the text itself into your question and format it as code by selecting it and pressing Ctrl-K or by using the editor's {} icon.
    – cas
    Commented Jun 24, 2021 at 0:21

1 Answer 1

2

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.

1
  • Thank you for helping.
    – Kapil
    Commented Jun 23, 2021 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .