If I run following Python script on Linux Mint 21 Cinnamon edition with Python 3.11.2:
import os
T = "gnome-terminal"
os.system(f'{T} -- python -ic"import os; os.system(\'resize -s 5 32\')"')
os.system(f'{T} -- bash -ic "resize -s 5 32"')
it creates following two Terminal windows:
UPDATE as reaction to a comment by Cyrus requesting to provide text instead of an image: if I replace the image with the text it would give in case of the first window:
COLUMNS=32;
LINES=5;
export COLUMNS LINES;
>>>
and in the case of the second window:
The child process exited normally with status 0.
making it as good as not possible to directly see what the question is about. Images are sometimes the much better option.
Notice that both of the script lines are doing the same job and are using the same command line options. The line using Python does not require a space between -ic
and the string. The line with bash does. In this context a question:
What is wrong with my expectation that both lines should give an interactive Terminal window?
The first line results as expected in an interactive Python session, but the second line fails to give an interactive session and wouldn't rise a Terminal window at all when in the Terminal settings the option to stay opened after the child process exits wouldn't be checked.
I would be glad if someone in course of giving an answer to my question could also explains why the line with bash needs the space where the Python line doesn't and how to tweak the code creating the interactive Python session so that it will fall back to a bash shell prompt instead of exiting the Terminal window if Python exits.
UPDATE as reaction to comment by Charles Duffy:
The info bash
command gives as an explanation of the -i option for bash: -i If the -i option is present, the shell is interactive. and man bash
gives the same explanation. Interactive when not running??? Has the word interactive
another meaning? Which one?
After reading the details of the bash -i option as suggested in the comments it turns out that the word interactive
is severe misused here and means the opposite of what I would expect from it. So if I want the shell to respond to the provided code I have to run the shell NON-interactive. OK. In this context it seems that my actual question should be:
> If your real question is ("how do I get bash to run some arbitrary code and then drop to an interactive shell?", then maybe ask that directly. – Charles Duffy
Below my attempt to use subprocess:
from subprocess import run
run([f'{T}', '--', 'python', '-ic', '"import os; os.system(\'resize -s 5 32\')"'])
run([f'{T}', ' -- bash ', '-i' , '-c', '"resize -s 5 32"'])
The code above does not resize the first window and gives following Error message on the second one:
# Failed to parse arguments: Unknown option -i
Corrected the code for the second window to:
run([f'{T}', '--', 'bash ' , '-i' , '-c', '"resize -s 5 32"'])
Now both windows are created without resize and the second one gives:
# Error: Failed to execute child process “bash ”: Failed to execve: No such file or directory