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 does the shell create a new thread to execute them?

  • 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, 2013 at 20:22
  • okay for example if the shell is bash, and command is 'cd', so how is it implemented? Jun 18, 2013 at 20:25

3 Answers 3


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 /

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


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.


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:


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.

  • 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 Jun 18, 2013 at 20:21
  • 2
    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, 2013 at 20:27
  • okay thank you for the clarification, i will check it Jun 18, 2013 at 20:29

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.