Table of contents

How to define functions

  1. Syntax

     function func_name {
         # nothing to do


     func_name() {
         # nothing to do
  2. For example

     function is_dir_git_repo {
         if git rev-parse --git-dir > /dev/null 2>&1; then
             echo 'using git repo'
             return 1
             echo 'not using git repo'
             return 0
     is_directory() {
         if [ -d "$1" ]; then
             return 1
             return 0
     is_directory() {
         [ -d "$1" ]

How to pass values to a function

  1. Parameter variables

    If no parameters are passed, the environment variable $# exists but has a value of 0.

    Belows are some parameter variables that we need to know.

    Parameter variable Description
    $0 The file name of the current script
    $1, $2, … The parameters given to the script
    $# The number of arguments supplied to a script
    $* A list of all the parameters, in a single variable, seperated by the first character in the environment variable IFS. If IFS is modified, then the way $* seperates the command line into parameters will change.
    $@ A subtle variation on $*; it does not use the IFS environment variable, so parameters are not run together even if IFS is empty
    $$ The process number of the current shell.
    $? The exit status of the last command
    $! The process number of the last background command
  2. Pass arguments and return value to function

    We supply the arguments directly after the function name. In function, we can access the value of arguments by using $1, $2, …

    We will use keyword return to return our something.

      print_value() {
       echo "Hello $1"
       return 10
     print_value world
     # Use #? contains the return status of the previously run command or function.
     echo "The returned value from above function is $?"
     # or
     value=$( print_value VietNam )

Some ways to return value from functions

  1. Using global variables

     function useGlobalVar() {
         myresult='hello, world'
     echo $myresult
  2. Define local variables and using command substitution

    • Command Substitution

      A command substitution means storing the output of a command execution in a variable.

      There are two ways to perform a command substitution:

      • Using the backtick character (‘)

          echo $dir
      • Using the dollar sign format - $()

          echo $dir
    • How to use

        function useLocalVar() {
            local localVar="Fuck you with local variables"
            echo "$localVar"
        echo $result 
  3. Using parameters as variables that receive result

     function passVarAsParam() {
         local _resultVar=$1
         local localVar='Hello, world! What do you want?'
         eval $_resultVar="'$localVar'"  # lack single quote in double quote string --> it is not working.
     passVarAsParam result
     echo $result
  4. Out of all above ways, we can use $? symbol to get the result from a command.

    $? symbol is the exit status of the last command. Exit status is a numerical value that is returned by a command.

     0 - successful
     1 - fail
     if [ "$EXIT_STATUS" -eq "0" ]
         # Do work when command exists on success
         # Do work for when command has a failure exit

    Or we can embedded a command in a condition of if, while statements.

     if run_some_command
         # Do work when command exists on success
         # Do failure exit work

    Use the second way is faster because we does not use temporary variable to receive our result.

Wrapping up

  • Understanding about how to pass value to a function, use these parameters in a function, and return values.