...
- Shell and sub-shell environments
- Program and function exit codes, and checking them
- Capturing standard output with parentheses evaluation
- Passing environment variables to a script using export
- Extending the current shell environment using source
- Testing source'd script functions with parentheses sub-shells
...
Every bash program has its own execution environment (sub-shell), which is a child process of its calling parent shell.
...
Parentheses evaluation is similar to backtick evaluation except that special syntax is needed to connect the standard output of backtick parenthesis evaluation to the standard input of the caller.
To capture the standard output of parentheses evaluation, the parentheses expression can be "evaluated" with a dollar sign ($). Consider:
- today=`date`
- today=$(date)
- because it is enclosed in parentheses, the date command is run in a sub-shell, writing its data to its standard output
- date's standard output stream is connected to the calling shell's standard input by the dollar sign ($) before the opening parenthesis.
- In both cases the caller's standard input text is stored in the today variable
...
A function can return this value using the return keyword (e.g. return 0
). No further code in the function is executed after return is called. The return value is then stored in the special $? variable, which can be checked by the caller.
Since this an exit code is not very much information, function return values are not often used or checkedfor significant data. Instead, as we've seen, functions are often called for their standard output, which serves as a return value proxy.
...
- No further code in the current sub-shell is executed after exit is called.
- A program's exit code is returned to the script caller (in the parent shell) in the $? variable.
- The default exit code for a script is the exit code of the last command executed.
- this will be the exit keyword's argument if exit is called explicitly
The main use of exit codes is to check that a called program completed successfully.
...
Note that in the non-0 exit code case, the program may also report error information on standard error (e.g. ls: cannot access not_a_file: No such file or directory
above).
...
Tip | ||
---|---|---|
| ||
We will do this in a new tmux or screen session, since accidentally calling exit at top-level (instead of in a sub-shell) will log you off the server! See this nice tmux cheat sheet: httphttps://atkinsamwww.pluralsight.com/resources/blog/documentscloud/tmux.pdf-cheat-sheet |
Code Block | ||
---|---|---|
| ||
# Invoke tmux from your login command line tmux new # Now you're in a tmux. Mine has a green bar at the bottom ( exit 0 ) echo $? ( exit 255 ) res=$? echo "exit code: $res" # exit tmux session exit # You're back at your login command line now |
...
Code Block | ||
---|---|---|
| ||
dat=$( cat not_a_file ) if [[ "$dat" == "" ]]; then echo "ERROR: no data found" 1>&2; exit 255 else echo "Data is: '$dat'" fi # or using -z to test for an empty string if [[ -z "$dat" ]]; then echo "ERROR: no data found" 1>&2; exit 255; else echo "Data is '$dat'"; fi |
...