September 2018
Beginner
186 pages
4h 30m
English
Like filesystem scripts or programs, Bash functions can have exit values. These are integer values normally used to describe how well the function did its job, often for use by the calling shell or program to decide what to do next after the function completes. In the case of functions, we will call these return values for clarity, as they use the return keyword rather than exit.
Here's an example of a simple pair of functions, succeed and fail; rather like true and false, they always return 0 and 1, respectively:
bash$ succeed() { return 0 ; }
bash$ fail() { return 1 ; }
As observed in Chapter 2, Bash Command Structure, we can test these using the special $? parameter:
bash$ succeed;echo $? 0 bash$ fail;echo ...
Read now
Unlock full access