Builtins are present as a way to have the system calls more faster. So, I believe read
command is present as a builtin to be more efficient.
Quoting from here,
These builtin commands are part of the shell, and are implemented as
part of the shell's source code. The shell recognizes that the command
that it was asked to execute was one of its builtins, and it performs
that action on its own, without calling out to a separate executable.
Different shells have different builtins, though there will be a whole
lot of overlap in the basic set.
Now, I would like this to be experimented by yourself, so that you can understand why read
is present as a shell builtin.
Normally, you couldn't do strace
on shell builtins. However, there is a workaround for this as well. This is explained pretty neatly in this answer.
- In the first shell, run the command as
stty -echo
.
- Open another shell and run the command as
cat | strace bash >
/dev/null
.
- Now, the shell would be waiting for the user to type in the commands
and there by when the user types the commands, you could see what
happens in the system level as well.
- When you give the above 3 commands, you could see that read has
fewer system calls than the remaining 2 commands. I am not pasting
the output from
strace
as it is pretty big.
read
can parse parameters separated by the internal field separator$IFS
. @ramesh'sstrace
trick is also an awesome way to analyze differences. – Tyler Sep 27 '14 at 19:24