ControlYourWay
ControlYourWay

Reputation: 11

Bash prompt not showing with custom Python terminal

I am writing a terminal program in Python (in Ubuntu) and I got the communication to Bash working well.
I use the following command to open pipes to Bash:

self.process = subprocess.Popen(['/bin/bash'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

I have seperate threads that handles the stdout and stderr data and I can send commands to stdin. The only thing I could not get working is that I receive a command prompt from Bash when a command is finished.

For example, if I list the directory content in an Ubuntu terminal the output is:
cyw@cyw-VirtualBox:~/testdir$ ls -l
total 0
-rw-rw-r-- 1 cyw cyw 0 Dec 1 15:55 file1
-rw-rw-r-- 1 cyw cyw 0 Dec 1 15:55 file2
-rw-rw-r-- 1 cyw cyw 0 Dec 1 15:55 file3
cyw@cyw-VirtualBox:~/testdir
$

The same output in my Python terminal looks as follows:
ls -l
-rw-rw-r-- 1 cyw cyw 0 Dec 1 15:55 file1
-rw-rw-r-- 1 cyw cyw 0 Dec 1 15:55 file2
-rw-rw-r-- 1 cyw cyw 0 Dec 1 15:55 file3

All my searching on Google mostly suggests editing the $PS1 variable but I don't think this is the problem here. Without the command prompt my terminal would be very hard to use. Any help would be appreciated.

Upvotes: 1

Views: 1057

Answers (2)

Alexander Lubyagin
Alexander Lubyagin

Reputation: 1494

Run bash in interactive mode for PS1:

  p = Popen(["/bin/bash","--norc","--noprofile","-i"], shell = False,
        stdin = PIPE, stdout = PIPE, stderr = STDOUT,
        bufsize = 1,
        env={"PS1":"\\u:\\h "},
        preexec_fn=os.setsid)

Upvotes: 0

ivan_pozdeev
ivan_pozdeev

Reputation: 36066

Your bash is not interactive.

From bash(1):

Prompting

When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command

Invocation

<...> An interactive shell is one started without non-option arguments 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.

Upvotes: 3

Related Questions