Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know that external commands are run in the shell by creating a separate process. But what exactly happens when a built in command is run in a shell. Are they executed as a function or if the shell creates a new thread to execute the built in command?

share|improve this question
    
That's going to be shell-specific. And it probably varies depending on the command (and possibly how its used in a pipe). –  derobert Jun 18 '13 at 20:22
    
okay for example if the shell is bash, and command is 'cd', so how is it implemented? –  corneliusfelix Jun 18 '13 at 20:25

3 Answers 3

up vote 3 down vote accepted

For your concrete example, there is a function cd_builtin, which is defined in builtins/cd.def (in the bash source code). It normally does a cd by calling that function. But it may fork first if you use it in a pipeline—for example, cd / | echo forks and calls cd_builtin in the child. You can also notice this by how the directory doesn't actually change:

anthony@Zia:~$ cd /tmp/
anthony@Zia:/tmp$ cd / | echo -n
anthony@Zia:/tmp$ cd /
anthony@Zia:/$ 

Notice how the directory only changes when I don't pipe from cd.

share|improve this answer

Built-in commands, by definition, are executed inside the main executable, as opposed to in a different program.

All shell commands are synchronous: the shell waits for the command to complete before it executes the next one. When running an external command, the shell has to create a separate process to run that command, and wait for it to exit. When running a built-in command, there is no need to create a new thread of execution: the command is executed inside the main thread.

I don't think any of the common shells use threads in their internal design. Shell capabilities are pretty tied with the traditional single-thread-per-process Unix model, there's no problem in shell design that threads would solve.

It's highly likely that every builtin is implemented by a function at some level in the shell's source code.

share|improve this answer
    
nice and thank. –  Tim Jul 12 at 0:37

I would think they run as a function within the space of the bash executable. There is a comment in the Advanced Bash Scripting Guide that states it as follows:

excerpt

A builtin is a command contained within the Bash tool set, literally built in. This is either for performance reasons -- builtins execute faster than external commands, which usually require forking off 1 a separate process -- or because a particular builtin needs direct access to the shell internals.

share|improve this answer
    
but here we aren't creating a new process, we are creating a new thread whose execution is faster than creating a process. i agree that function implementation is faster than thread creation. But the scenario here is this for the execution time: process creation>thread creation>function calling. We can't be sure whether it is thread or function –  corneliusfelix Jun 18 '13 at 20:21
    
I think you're getting hung up on semantics. I'm pretty sure these are serviced by function calls by bash. My reasoning is that if I watch my bash process using a tool such as htop configured to show threads, there aren't any when I run the different built-in commands to bash. –  slm Jun 18 '13 at 20:27
    
okay thank you for the clarification, i will check it –  corneliusfelix Jun 18 '13 at 20:29

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.