From your comments, you seem to be confused about exactly what a shell is. The kernel is responsible for managing the system. It's the part that actually loads and runs programs, accesses files, allocates memory, etc. But the kernel has no user interface; you can only communicate with it by using another program as an intermediary.
A shell is a program that prints a prompt, reads a line of input from you, and then interprets it as one or more commands to manipulate files or run other programs. Before the invention of the GUI, the shell was the primary user interface of an OS. On MS-DOS, the shell was called command.com
and few people ever tried to use a different one. On Unix, however, there have long been multiple shells that users could pick from.
They can be divided into 3 types. The Bourne-compatible shells use the syntax derived from the original Bourne shell. C shells use the syntax from the original C shell. Then there are nontraditional shells that invent their own syntax, or borrow one from some programming language, and are generally much less popular than the first two types.
A built-in command is simply a command that the shell carries out itself, instead of interpreting it as a request to load and run some other program. This has two main effects. First, it's usually faster, because loading and running a program takes time. Of course, the longer the command takes to run, the less significant the load time is compared to the overall run time (because the load time is fairly constant).
Secondly, a built-in command can affect the internal state of the shell. That's why commands like cd
must be built-in, because an external program can't change the current directory of the shell. Other commands, like echo
, might be built-in for efficiency, but there's no intrinsic reason they can't be external commands.
Which commands are built-in depends on the shell that you're using. You'll have to consult its documentation for a list (e.g., bash
's built-in commands are listed in Chapter 4 of its manual). The type
command can tell you if a command is built-in (if your shell is POSIX-compatible), because POSIX requires that type
be a built-in. If which
is not a built-in in your shell, then it probably won't know about your shell's built-ins, but will just look for external programs.
./test.sh
runs test.sh
as a separate program. It may happen to be a bash script, if the file test.sh
starts with #!/bin/bash
. But it could be something else altogether.
. ./test.sh
executes the code of the file test.sh
inside the running instance of bash. It works as if the content file test.sh
had been included textually instead of the . ./test.sh
line. (Almost: there are a few details that differ, such as the value of $BASH_LINENO
, and the behavior of the return
builtin.)
source ./test.sh
is identical to . ./test.sh
in bash (in other shells, source
may be slightly different or not exist altogether; .
for inclusion is in the POSIX standard).
The most commonly visible difference between running a separate script with ./test.sh
and including a script with the .
builtin is that if the test.sh
script sets some environment variables, with a separate process, only the environment of the child process is set, whereas with script inclusion, the environment of the sole shell process is set. If you add a line foo=bar
in test.sh
and echo $foo
at the end of the calling script, you'll see the difference:
$ cat test.sh
#!/bin/sh
foo=bar
$ ./test.sh
$ echo $foo
$ . ./test.sh
$ echo $foo
bar
Best Answer
The
cd
command is a built-in, so normallybuiltin cd
will do the same thing ascd
. But there is a difference ifcd
is redefined as a function or alias, in which casecd
will call the function/alias butbuiltin cd
will still change the directory (in other words, will keep the built-in accessible even if clobbered by a function.)For example:
Or with an alias:
Using
builtin
is also a good way to define acd
function that does something and changes directory (since callingcd
from it would just keep calling the function again in an endless recursion.)For example: