1

I was trying to create new session without having to logout and login, so thought exec would help. But got following

[root@vanhalen ~]# echo $$
46144
[root@vanhalen ~]# exec bash
[root@vanhalen ~]# echo $$
46144

I was expecting different $$ value for second output since new bash process should replace old. what is the behavior here ?

1 Answer 1

2

$$ is the process ID of the process in which the shell is running. It would change only if a new process were created.

From the bash manpage, about exec:

exec [-cl] [-a name] [command [arguments]]

If command is specified, it replaces the shell. No new process is created. (...)

What this means is that exec behaves very much like the C function execve and its various frontends (indeed the shell very likely uses one of them) in that the process image of the calling process is replaced with one describing the specified command, and exec never returns. All this happens without spawning a new process; the old shell simply ends.

The new shell, running in the old process, will run through all the usual startup motions, though (your .bashrc is sourced and all that). Whether this is enough for a new session depends on your understanding of the term "session."

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. By new session I meant all process created by this bash to be killed and a new terminal assigned(logout + login achieves this). since exec is not creating new process, this problem is still there..
For that you'd have to keep track of all processes the shell ever spawned, and their subprocesses (in case of daemons). That can be achieved on Linux with cgroups, but I think this is a question for Superuser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.