Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are builtin commands implemented in shell?

When a shell (e.g. bash) invokes an executable file, it first fork itself, and then its copy execve the executable file.

When a shell invokes builtin commands, there is no new process created, and execve can only operate on executable files while builtin commands are not stored in executable files.

So how are builtin commands stored, and how are they invoked in terms of system calls?

like image 429
Tim Avatar asked Oct 31 '22 05:10

Tim


2 Answers

"builtin command" means that you don't have to run an external program. So, no, there's no execve involved at all, and no, there's not even any system call necessarily involved. Your shell really just parses a command string and sees "hey, that's a builtin command, let's execute this and that function".

like image 145
Marcus Müller Avatar answered Nov 02 '22 09:11

Marcus Müller


You can imagine they are the same as shell functions.

So instead of launching external process the shell invokes some internal function library function which reads the input outputs the result and does pretty much the same as main function of regular program.

like image 37
Zbynek Vyskovsky - kvr000 Avatar answered Nov 02 '22 11:11

Zbynek Vyskovsky - kvr000