简体   繁体   中英

exec not bash process with another bash

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 ?

$$ 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM