Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Every bash program has its own execution environment (sub-shell), which is a child process of its calling parent shell. Every shell and sub-shell is actually an entirely separate process.

Here are the main communication methods between shell execution environments:

  • Input to sub-shells
    • program arguments
    • environment variables
    • standard input stream
    • file data
  • Output from sub-shells
    • exit code
    • standard output and standard error streams
    • file data

A new sub-shell (child shell) is created, runs, and returns whenever:

  • a built-in bash utility (e.g. ls) is run from the command line (or from within script)
  • a custom script (e.g. step_03.sh) or program is run from the command line (or from within a script)
  • backtick evaluation is used to execute commands (e.g. echo `date`)
    • the date function runs in a sub-shell, whose results are provided to the echo function's sub-shell
  • any set of commands enclosed in parentheses is run, e.g.
    • ( date )

...

A parenthesis sub-shell is a new, child shell, created when a command is enclosed in parentheses ( ). Note that any environment variables set in a parenthesis sub-shell are not visible to the parent.

Code Block
baz=abc
echo $baz

(baz=xya)
echo $baz  # will stilstill be abdabc

Parentheses evaluation

Parentheses evaluation is similar to backtick evaluation except that special syntax is needed to connect the standard output of parenthesis evaluation to the standard input of the caller.

...

A function can return this value using the return keyword (e.g. return 0 or return 255). No further code in the function is executed after return is called. The return value code is is then stored in the special $? variable, which can be checked by the caller.

Since an exit a return code is not very much information, function return values are not often used for significant data. Instead, as we've seen, functions are often called for their standard output, which serves as a return value proxy.

A script can also return an specific integer value, called the exit code, using the exit keyword (e.g. exit 255)

...