How to keep the terminal open after executing a C program?

5,643

To keep the terminal opened until a key is pressed:

gnome-terminal -- bash -c "ls && read"

To keep the terminal opened until exit:

gnome-terminal -- bash -c "ls && bash"

Replace ls with the command you want to execute (your compiled executable file).

Note

If -- does not work, try the old (deprecated) -x approach instead:

gnome-terminal -x bash [...]
Share:
5,643
Pallab Pain
Author by

Pallab Pain

Updated on September 18, 2022

Comments

  • Pallab Pain
    Pallab Pain almost 2 years

    I'm trying to run a C program from within a python code.

    cmd = 'gnome-terminal --command=./myprog'
    subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    

    The code executes and the terminal closes.

    I want to keep the terminal open after the execution is completed without having to add any extra code in the C file e.g. prompt the user for some input.

    Is there a workaround for this?

  • Pallab Pain
    Pallab Pain about 9 years
    Yeah, it didn't work with gnome-terminal.
  • Pallab Pain
    Pallab Pain about 9 years
    Of course, but I don't seem to have enough reputation points to upvote an answer as of now. I'll definitely upvote once I meet the criteria. :)
  • Peque
    Peque about 9 years
    @pallab.pain: hahaha. Ok, don't worry then, its just that many times there's people registering just to make one question and then they leave it opened (without an accepted answer) forever. You can't accept the answer either with low reputation? (there should be a green tick bellow the upvote/downvote buttons) That way people will know the problem was solved and will be able to spend their time on other non-answered questions. ;-)
  • CodyBugstein
    CodyBugstein over 7 years
    Doesn't seem to be an option -c for gnome-terminal
  • Peque
    Peque over 7 years
    @CodyBugstein: -c is for the bash command. ;-)
  • mtkilic
    mtkilic almost 6 years
    @Peque how can I stay in same terminal(newly opened) and run other commands
  • Peque
    Peque almost 6 years
    @mtkilic Try gnome-terminal -- bash -c "ls && bash".
  • mtkilic
    mtkilic almost 6 years
    @Peque Looks like this worked for me subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', 'ls && whoami && cd /tmp && bash'], env=os.environ) but I dont like how I have pass all my commands in one line of code.
  • MrCholo
    MrCholo over 5 years
    this works, but I can't seem to get back to a command line. What I would want is to be able to get back to the command line with ctrl-c, and if I wanted to re-run the command I would use bash history / up arrow.