mora mora - 1 year ago 74
Bash Question

What does builtin commands means in bash?

I read a part of manual of bash. The item is "COMMAND EXECUTION ENVIRONMENT". The part says,

Builtin commands that are invoked as part of a pipeline are also executed in a subshell environment. Changes made to the subshell environment cannot affect the shell's execution environment.

I suppose it means value changed in pipeline is local because each command in pipeline runs in its own sub-shell. Like following,

echo "Before pipe, ${value}"
value='2' | echo "${value}" | value='3' | echo "In another pipe, ${value}"
echo "After pipe, ${value}"

Before pipe, 1
In another pipe, 1
After pipe, 1

I read "SHELL BUILTIN COMMANDS" in bash. But I could not find "=" as builtin command. What does "builtin commands" means here? And are there "non-builtin commands" which can affect the change globally even in pipe-line?

And if you don't mind please let me know when the new sub-shell runs except for:

  1. (...)

  2. pipeline

Answer Source

I think that the manual is basically saying that built-in commands, such as echo, printf, read, etc. don't get any special treatment and still run within their own sub-shell, even though in principal it would be possible for the shell to determine that all of the commands in the pipeline could be run natively in the same shell.

If you ask to pipe one command into another, then sub-shells are created, no matter what is on either side of the pipe.

For example:

echo string | read foo

uses the two built-ins, echo and read but the variable $foo ceases to exist after the pipeline finishes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download