Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I call:

execl("/bin/sh", "/bin/sh", (void *) NULL);

The /bin/sh/ program is started, which opens a shell in which I can type commands. However, if I call:

execl("/bin/sh", "/bin/sh", "~/somescript.sh", (char *) 0 );

The /bin/sh program is also started and runs the given shell script but then closes afterwards.

Why doesn't the shell stay open after running a shell script? Is there anyway to keep it open after running the script?

Thanks in advance!

share|improve this question

2 Answers 2

This is a normal shell behaviour that doesn't have anything in common with the fact that you run it from C - it happens even if you run the shell with the script from command line:

$ sh ~/somescript.sh

It might be difficult to force shell to stay in interactive mode after that. I found only the --rcfile [file] option to do that:

Execute commands from file instead of the system wide initialization file /etc/bash.bashrc and the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).

I.e. you would do

$ sh --rcfile ~/somescript.sh

But your system rc files like /etc/bash.bashrc and ~/.bashrc will not be run in this case.

share|improve this answer

How about running an interactive shell at the end of your script?

#!/bin/sh

echo STUFF
echo MORE STUFF
sh
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.