1447

I am trying to search how to pass parameters in a Bash function, but what comes up is always how to pass parameter from the command line.

I would like to pass parameters within my script. I tried:

myBackupFunction("..", "...", "xx")

function myBackupFunction($directory, $options, $rootPassword) {
     ...
}

But the syntax is not correct. How can I pass a parameter to my function?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
stivlo
  • 83,644
  • 31
  • 142
  • 199
  • 13
    "...but what comes up is always how to pass parameter from the command line" - Yes! That's because Bash scripts are basically sequences of command lines - invoke a function in a Bash script exactly as if it was a command on the command line! :-) Your call would be myBackupFunction ".." "..." "xx"; no parenthesis, no commas. – Wil Oct 20 '17 at 06:33
  • 5
    The counterpart to this question: [return value from a bash function](https://stackoverflow.com/questions/17336915/return-value-in-a-bash-function) – MSalters Sep 17 '18 at 15:24
  • See also: [Passing arrays as parameters in bash](https://stackoverflow.com/q/1063347/4561887) – Gabriel Staples Jan 25 '22 at 16:41

7 Answers7

2219

There are two typical ways of declaring a function. I prefer the second approach.

function function_name {
   command...
} 

or

function_name () {
   command...
} 

To call a function with arguments:

function_name "$arg1" "$arg2"

The function refers to passed arguments by their position (not by name), that is $1, $2, and so forth. $0 is the name of the script itself.

Example:

function_name () {
   echo "Parameter #1 is $1"
}

Also, you need to call your function after it is declared.

#!/usr/bin/env sh

foo 1  # this will fail because foo has not been declared yet.

foo() {
    echo "Parameter #1 is $1"
}

foo 2 # this will work.

Output:

./myScript.sh: line 2: foo: command not found
Parameter #1 is 2

Reference: Advanced Bash-Scripting Guide.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 2
    When I write `function name(){}` I get an error about the '('. But when I write `name(){}` it works. Any ideas? – CMCDragonkai Nov 10 '13 at 18:33
  • 6
    You have forgotten the spaces, try `function name() {}`. Maybe with a 'enter' before `{}` – Leandro Nov 11 '13 at 14:09
  • 34
    Good answer. My 2 cents: in shell constructs that reside in a file that is sourced (dotted) when needed, I prefer to use the `function` keyword __and__ the `()`. My goal (in a file, not command line) is to increase clarity, not reduce the number of characters typed, viz, `function myBackupFunction() compound-statement`. – Terry Gardner Nov 27 '13 at 17:25
  • 37
    @CMCDragonkai, the `function` keyword version is an extension; the other form works in all POSIX-compliant shells. – Charles Duffy May 04 '15 at 17:02
  • 21
    @TerryGardner, consider that your attempts to increase clarity are reducing compatibility. – Charles Duffy May 04 '15 at 17:02
  • 3
    Of course, if you REALLY restrict yourself, you can make your script compatible with MS-DOS COMMAND.EXE. Perhaps compatibility is only situationally relevant. – Ron Burk Apr 13 '16 at 05:27
  • 12
    @RonBurk, perhaps -- but even if we consider only clarity, the `function` keyword had guarantees in the old ksh-family shells that introduced it that modern bash don't honor (in such shells, `function` made variables local-by-default; in bash, it does not). As such, its use *decreases* clarity to anyone who knows, and might expect, the ksh behavior. See http://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Feb 28 '18 at 21:31
  • 1
    @dogbane, would you be willing to consider changing the reference link to a different source -- be that the official manual, the bash-hackers' wiki, or the Wooledge wiki -- or to accept an edit which does so? The ABS has quite a history of teaching people bad practices through examples that aren't carefully chosen to showcase good behavior. – Charles Duffy Sep 19 '18 at 03:19
  • 1
    also, I want to add, that you could use `$#` to get the count of the arguments, passed to the function. – almaceleste Mar 02 '20 at 23:58
  • What if first argument is a string array? – Jaraws Nov 11 '20 at 09:31
  • How do I make the function be recognized by `xargs`? It seems that the function works when I call it myself like `myfunc hi` but if I use `| xargs myfunc` it doesn't work. – Aaron Franke May 22 '22 at 06:27
167

Knowledge of high level programming languages (C/C++, Java, PHP, Python, Perl, etc.) would suggest to the layman that Bourne Again Shell (Bash) functions should work like they do in those other languages.

Instead, Bash functions work like shell commands and expect arguments to be passed to them in the same way one might pass an option to a shell command (e.g. ls -l). In effect, function arguments in Bash are treated as positional parameters ($1, $2..$9, ${10}, ${11}, and so on). This is no surprise considering how getopts works. Do not use parentheses to call a function in Bash.


(Note: I happen to be working on OpenSolaris at the moment.)

# Bash style declaration for all you PHP/JavaScript junkies. :-)
# $1 is the directory to archive
# $2 is the name of the tar and zipped file when all is done.
function backupWebRoot ()
{
    tar -cvf - "$1" | zip -n .jpg:.gif:.png "$2" - 2>> $errorlog &&
        echo -e "\nTarball created!\n"
}


# sh style declaration for the purist in you. ;-)
# $1 is the directory to archive
# $2 is the name of the tar and zipped file when all is done.
backupWebRoot ()
{
    tar -cvf - "$1" | zip -n .jpg:.gif:.png "$2" - 2>> $errorlog &&
        echo -e "\nTarball created!\n"
}


# In the actual shell script
# $0               $1            $2

backupWebRoot ~/public/www/ webSite.tar.zip

Want to use names for variables? Just do something this.

local filename=$1 # The keyword declare can be used, but local is semantically more specific.

Be careful, though. If an argument to a function has a space in it, you may want to do this instead! Otherwise, $1 might not be what you think it is.

local filename="$1" # Just to be on the safe side. Although, if $1 was an integer, then what? Is that even possible? Humm.

Want to pass an array to a function by value?

callingSomeFunction "${someArray[@]}" # Expands to all array elements.

Inside the function, handle the arguments like this.

function callingSomeFunction ()
{
    for value in "$@" # You want to use "$@" here, not "$*" !!!!!
    do
        :
    done
}

Need to pass a value and an array, but still use "$@" inside the function?

function linearSearch ()
{
    local myVar="$1"

    shift 1 # Removes $1 from the parameter list

    for value in "$@" # Represents the remaining parameters.
    do
        if [[ $value == $myVar ]]
        then
            echo -e "Found it!\t... after a while."
            return 0
        fi
    done

    return 1
}

linearSearch $someStringValue "${someArray[@]}"

In Bash 4.3 and above, you can pass an array to a function by reference by defining the parameter of a function with the -n option.

function callingSomeFunction ()
{
    local -n someArray=$1 # also ${1:?} to make the parameter mandatory.

    for value in "${someArray[@]}" # Nice!
    do
        :
    done
}

callingSomeFunction myArray # No $ in front of the argument. You pass by name, not expansion / value.
Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44
  • The last example posted does not work as far as I can tell. I tried to run it on bash v5+ and it is just returning me the full array in the loop as opposed to each item – iomv Nov 25 '21 at 17:00
  • 1
    after testing again, I found that it was my error as I was declaring the array in line instead of declaring it before – iomv Nov 25 '21 at 17:19
  • 3
    @iomv Nonetheless, do be careful of the "circular variable reference" problem. Whatever name you declare the array as inside of the function, DO NOT name your array argument in the calling context / client code the same name. Notice how I changed the last example to help people avoid the "circular name reference" problem. Good call, even though you made an error on your own. :-) – Anthony Rutledge Nov 25 '21 at 19:07
84

If you prefer named parameters, it's possible (with a few tricks) to actually pass named parameters to functions (also makes it possible to pass arrays and references).

The method I developed allows you to define named parameters passed to a function like this:

function example { args : string firstName , string lastName , integer age } {
  echo "My name is ${firstName} ${lastName} and I am ${age} years old."
}

You can also annotate arguments as @required or @readonly, create ...rest arguments, create arrays from sequential arguments (using e.g. string[4]) and optionally list the arguments in multiple lines:

function example {
  args
    : @required string firstName
    : string lastName
    : integer age
    : string[] ...favoriteHobbies

  echo "My name is ${firstName} ${lastName} and I am ${age} years old."
  echo "My favorite hobbies include: ${favoriteHobbies[*]}"
}

In other words, not only you can call your parameters by their names (which makes up for a more readable core), you can actually pass arrays (and references to variables - this feature works only in Bash 4.3 though)! Plus, the mapped variables are all in the local scope, just as $1 (and others).

The code that makes this work is pretty light and works both in Bash 3 and Bash 4 (these are the only versions I've tested it with). If you're interested in more tricks like this that make developing with bash much nicer and easier, you can take a look at my Bash Infinity Framework, the code below is available as one of its functionalities.

shopt -s expand_aliases

function assignTrap {
  local evalString
  local -i paramIndex=${__paramIndex-0}
  local initialCommand="${1-}"

  if [[ "$initialCommand" != ":" ]]
  then
    echo "trap - DEBUG; eval \"${__previousTrap}\"; unset __previousTrap; unset __paramIndex;"
    return
  fi

  while [[ "${1-}" == "," || "${1-}" == "${initialCommand}" ]] || [[ "${#@}" -gt 0 && "$paramIndex" -eq 0 ]]
  do
    shift # First colon ":" or next parameter's comma ","
    paramIndex+=1
    local -a decorators=()
    while [[ "${1-}" == "@"* ]]
    do
      decorators+=( "$1" )
      shift
    done

    local declaration=
    local wrapLeft='"'
    local wrapRight='"'
    local nextType="$1"
    local length=1

    case ${nextType} in
      string | boolean) declaration="local " ;;
      integer) declaration="local -i" ;;
      reference) declaration="local -n" ;;
      arrayDeclaration) declaration="local -a"; wrapLeft= ; wrapRight= ;;
      assocDeclaration) declaration="local -A"; wrapLeft= ; wrapRight= ;;
      "string["*"]") declaration="local -a"; length="${nextType//[a-z\[\]]}" ;;
      "integer["*"]") declaration="local -ai"; length="${nextType//[a-z\[\]]}" ;;
    esac

    if [[ "${declaration}" != "" ]]
    then
      shift
      local nextName="$1"

      for decorator in "${decorators[@]}"
      do
        case ${decorator} in
          @readonly) declaration+="r" ;;
          @required) evalString+="[[ ! -z \$${paramIndex} ]] || echo \"Parameter '$nextName' ($nextType) is marked as required by '${FUNCNAME[1]}' function.\"; " >&2 ;;
          @global) declaration+="g" ;;
        esac
      done

      local paramRange="$paramIndex"

      if [[ -z "$length" ]]
      then
        # ...rest
        paramRange="{@:$paramIndex}"
        # trim leading ...
        nextName="${nextName//\./}"
        if [[ "${#@}" -gt 1 ]]
        then
          echo "Unexpected arguments after a rest array ($nextName) in '${FUNCNAME[1]}' function." >&2
        fi
      elif [[ "$length" -gt 1 ]]
      then
        paramRange="{@:$paramIndex:$length}"
        paramIndex+=$((length - 1))
      fi

      evalString+="${declaration} ${nextName}=${wrapLeft}\$${paramRange}${wrapRight}; "

      # Continue to the next parameter:
      shift
    fi
  done
  echo "${evalString} local -i __paramIndex=${paramIndex};"
}

alias args='local __previousTrap=$(trap -p DEBUG); trap "eval \"\$(assignTrap \$BASH_COMMAND)\";" DEBUG;'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
niieani
  • 4,101
  • 1
  • 31
  • 22
  • What are the `@var`, `@reference`, `@params` variables ? What should I look up on internet to learn more about this? – GypsyCosmonaut Aug 06 '17 at 07:43
  • 1
    Hi @niieani when I try to create a bash function in the form you use in your answer it tells me I need to install ucommon utils from apt. Is this how your bash script works? Am I doing this correctly? If I understand you or someone else basically built the ucommon util program to allow for an extension of Bash, correct? – Aslan French Jul 18 '18 at 18:10
  • @DavidA.French no, this shouldn't happen. There is no relation between `ucommon` and my code. It's possible you have some tool installed which causes the issue you mentioned, no idea what could it be. – niieani Aug 02 '18 at 06:23
  • 1
    Far too involved, given the question.Things like `local filename=$1` work well enough for most. More over, in bash, one has the option to use declare `-A` to create associative arrays. You can already pass arrays as a list! `callingSomeFunction "${someArray[@]}"` – Anthony Rutledge Mar 13 '20 at 13:27
55

Drop the parentheses and commas:

 myBackupFunction ".." "..." "xx"

And the function should look like this:

function myBackupFunction() {
    # Here $1 is the first parameter, $2 the second, etc.
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
20

A simple example that will clear both during executing script or inside script while calling a function.

#!/bin/bash
echo "parameterized function example"
function print_param_value(){
    value1="${1}" # $1 represent first argument
    value2="${2}" # $2 represent second argument
    echo "param 1 is  ${value1}" # As string
    echo "param 2 is ${value2}"
    sum=$(($value1+$value2)) # Process them as number
    echo "The sum of two value is ${sum}"
}
print_param_value "6" "4" # Space-separated value
# You can also pass parameters during executing the script
print_param_value "$1" "$2" # Parameter $1 and $2 during execution

# Suppose our script name is "param_example".
# Call it like this:
#
# ./param_example 5 5
#
# Now the parameters will be $1=5 and $2=5
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • For what its worth I follow this form as well but I am wondering is there any added benefit? – tijko Dec 14 '22 at 13:59
14

It takes two numbers from the user, feeds them to the function called add (in the very last line of the code), and add will sum them up and print them.

#!/bin/bash

read -p "Enter the first  value: " x
read -p "Enter the second value: " y

add(){
    arg1=$1 # arg1 gets to be the first  assigned argument (note there are no spaces)
      arg2=$2 # arg2 gets to be the second assigned argument (note there are no spaces)

    echo $(($arg1 + $arg2))
}

add x y # Feeding the arguments
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Milad P.
  • 4,707
  • 3
  • 12
  • 9
  • 8
    Passing by name in that manner only works for integers passed into the numeric operator (( )), and it only works because the numeric operator recursively resolves strings to values. If you'd like to test what I mean, try entering '5' for x and then 'x' for y and you'll see that it adds (x + y ) = ( 5 + x ) = ( 5 + 5 ) = 10. For all other use cases your example will fail. Instead you should use 'add "$x" "$y"' for generic code. – Wil Oct 20 '17 at 06:24
10

Another way to pass named parameters to Bash... is passing by reference. This is supported as of Bash 4.0

#!/bin/bash
function myBackupFunction(){ # directory options destination filename
local directory="$1" options="$2" destination="$3" filename="$4";
  echo "tar cz ${!options} ${!directory} | ssh root@backupserver \"cat > /mnt/${!destination}/${!filename}.tgz\"";
}

declare -A backup=([directory]=".." [options]="..." [destination]="backups" [filename]="backup" );

myBackupFunction backup[directory] backup[options] backup[destination] backup[filename];

An alternative syntax for Bash 4.3 is using a nameref.

Although the nameref is a lot more convenient in that it seamlessly dereferences, some older supported distros still ship an older version, so I won't recommend it quite yet.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wil
  • 757
  • 9
  • 12