Chapter 13

  1. In which two ways can we define a function?
    • name() {
      }
    • function name {
      }
  2. What are some advantages of functions?
    • Easy to reuse code
    • Facilitates code sharing
    • Abstracting complex code
  3. What is the difference between a globally scoped variable and a locally scoped one?
    Locally scoped variables are only valid within a function, globally scoped variables can be used throughout the entire script (even in functions).
  4. How can we set values and attributes on variables?
    By using the declare command.
  5. How can a function use arguments passed to it?
    In the same way as a script can: by using $1, $#, $@, and so on.
  6. How can we return a value from a function?
    By outputting it to stdout. The command that calls the function should know to capture the output, using command substitution.
  7. What does the source command do?
    It executes commands from a file in the current shell. If the sourced file contains only function definitions, these are loaded to be used later (but still only in the current shell).
  8. Why would we want to create a function library?
    A lot of utility functions, such as argument checking, error handling and color setting, never change and can sometimes be complex to figure out. If we do this once properly, we can then use the predefined functions from our library without needing to duplicate code from older scripts.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.60.220