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!

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.


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


echo STUFF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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