Is there a way to avoid positional arguments in bash?

bash script arguments
bash positional parameters
bash getopts
bash named arguments
bash function arguments
bash return from function
bash getopts multiple arguments
bash check if argument exists

I have to write a function in bash. The function will take about 7 arguments. I know that I can call a function like this:

To call a function with parameters:

function_name $arg1 $arg2

And I can refer my parameters like this inside the function:

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

My question is, is there a better way refer to the parameters inside the function? Can I avoid the $1, $2, $3, .... thing and simply use the $arg1, $arg2, ...?

Is there a proper method for this or do I need to re-assign these parameters to some other variables inside the function? E.g.:

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

Any example would be much appreciated.

The common way of doing that is assigning the arguments to local variables in the function, i.e.:

copy() {
    local from=${1}
    local to=${2}

    # ...
}

Another solution may be getopt-style option parsing.

copy() {
    local arg from to
    while getopts 'f:t:' arg
    do
        case ${arg} in
            f) from=${OPTARG};;
            t) to=${OPTARG};;
            *) return 1 # illegal option
        esac
    done
}

copy -f /tmp/a -t /tmp/b

Sadly, bash can't handle long options which would be more readable, i.e.:

copy --from /tmp/a --to /tmp/b

For that, you either need to use the external getopt program (which I think has long option support only on GNU systems) or implement the long option parser by hand, i.e.:

copy() {
    local from to

    while [[ ${1} ]]; do
        case "${1}" in
            --from)
                from=${2}
                shift
                ;;
            --to)
                to=${2}
                shift
                ;;
            *)
                echo "Unknown parameter: ${1}" >&2
                return 1
        esac

        if ! shift; then
            echo 'Missing parameter argument.' >&2
            return 1
        fi
    done
}

copy --from /tmp/a --to /tmp/b

Also see: using getopts in bash shell script to get long and short command line options


You can also be lazy, and just pass the 'variables' as arguments to the function, i.e.:

copy() {
    local "${@}"

    # ...
}

copy from=/tmp/a to=/tmp/b

and you'll have ${from} and ${to} in the function as local variables.

Just note that the same issue as below applies — if a particular variable is not passed, it will be inherited from parent environment. You may want to add a 'safety line' like:

copy() {
    local from to    # reset first
    local "${@}"

    # ...
}

to ensure that ${from} and ${to} will be unset when not passed.


And if something very bad is of your interest, you could also assign the arguments as global variables when invoking the function, i.e.:

from=/tmp/a to=/tmp/b copy

Then you could just use ${from} and ${to} within the copy() function. Just note that you should then always pass all parameters. Otherwise, a random variable may leak into the function.

from= to=/tmp/b copy   # safe
to=/tmp/b copy         # unsafe: ${from} may be declared elsewhere

If you have bash 4.1 (I think), you can also try using associative arrays. It will allow you to pass named arguments but it will be ugly. Something like:

args=( [from]=/tmp/a [to]=/tmp/b )
copy args

And then in copy(), you'd need to grab the array.

Special Variable Types, Updating or adding new environmental variables causes the shell to update its for positional parameters leads to a fairly simple way of referencing the last One way to prevent this is to append an extra character to both sides of the� Bash Positional Parameters, Arguments Bash Shell has several special positional parameters which can be referenced but can not be assigned. These bash parameters are used to process command line arguments in a bash shell script, to get process status, exit status and options flag.

You can always pass things through the environment:

#!/bin/sh
foo() {
  echo arg1 = $arg1
  echo arg2 = $arg2
}

arg1=banana arg2=apple foo

Shell Variables, Positional parameters have the names 1, 2, 3, etc., meaning that their values are in turn means that each function needs to keep track of positional parameters� The most common way to handle this is to use local variables for arguments and any temporary variable within a function: example() { local A="$1" B="$2" C="$3" TMP="/tmp" This avoids polluting the shell namespace with function-local variables.

Shell functions have full access to any variable available in their calling scope, except for those variable names that are used as local variables inside the function itself. In addition, any non-local variable set within a function is available on the outside after the function has been called. Consider the following example:

A=aaa
B=bbb

echo "A=$A B=$B C=$C"

example() {
    echo "example(): A=$A B=$B C=$C"

    A=AAA
    local B=BBB
    C=CCC

    echo "example(): A=$A B=$B C=$C"
}

example

echo "A=$A B=$B C=$C"

This snippet has the following output:

A=aaa B=bbb C=
example(): A=aaa B=bbb C=
example(): A=AAA B=BBB C=CCC
A=AAA B=bbb C=CCC

The obvious disadvantage of this approach is that functions are not self-contained any more and that setting a variable outside a function may have unintended side-effects. It would also make things harder if you wanted to pass data to a function without assigning it to a variable first, since this function is not using positional parameters any more.

The most common way to handle this is to use local variables for arguments and any temporary variable within a function:

example() {
   local A="$1" B="$2" C="$3" TMP="/tmp"

   ...
}

This avoids polluting the shell namespace with function-local variables.

Bash Functions, The purpose of a function is to help you make your bash scripts more readable and to avoid writing the same repeatedly. To better illustrate how variables scope works in Bash, let's consider this example: The $# variable holds the number of positional parameters/arguments passed to the function. Where test/ is my first positional parameter and bash/ is my second positional parameter. In this way, we can send variables to a shell script on the fly. For people who want to see all these variables in one place, below list is for you.

All you have to do is name variables on the way in to the function call.

function test() {
    echo $a
}

a='hello world' test
#prove variable didnt leak
echo $a .

This isn't just a feature of functions, you could have that function in it's own script and call a='hello world' test.sh and it would work just the same


As an extra little bit of fun, you can combine this method with positional arguments (say you were making a script and some users mightn't know the variable names). Heck, why not let it have defaults for those arguments too? Well sure, easy peasy!

function test2() {
    [[ -n "$1" ]] && local a="$1"; [[ -z "$a" ]] && local a='hi'
    [[ -n "$2" ]] && local b="$2"; [[ -z "$b" ]] && local b='bye'
    echo $a $b
}

#see the defaults
test2

#use positional as usual
test2 '' there
#use named parameter
a=well test2
#mix it up
b=one test2 nice

#prove variables didnt leak
echo $a $b .

Note that if test was its own script, you don't need to use the local keyword.

Bash Positional Parameters, Arguments, These bash positional parameters can be assigned to a variable and values can be used for $0 : bash Shell argument 0, It expands into bash script file name or bash shell. the locations of commands it has found through querying your PATH. Six Common Mistakes For JavaScript Programmers To Avoid � AI & Search� Yes, the point is to get too the command line arguments from inside a function without passing them in as functional arguments. It has to do with an error handling situation in which I want to do error handling based on command line arguments independently of the arguments passed into the function. – DonGar May 9 '10 at 23:13

I think I have a solution for you. With a few tricks you can actually pass named parameters to functions, along with arrays.

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

testPassingParams() {

    @var hello
    l=4 @array anArrayWithFourElements
    l=2 @array anotherArrayWithTwo
    @var anotherSingle
    @reference table   # references only work in bash >=4.3
    @params anArrayOfVariedSize

    test "$hello" = "$1" && echo correct
    #
    test "${anArrayWithFourElements[0]}" = "$2" && echo correct
    test "${anArrayWithFourElements[1]}" = "$3" && echo correct
    test "${anArrayWithFourElements[2]}" = "$4" && echo correct
    # etc...
    #
    test "${anotherArrayWithTwo[0]}" = "$6" && echo correct
    test "${anotherArrayWithTwo[1]}" = "$7" && echo correct
    #
    test "$anotherSingle" = "$8" && echo correct
    #
    test "${table[test]}" = "works"
    table[inside]="adding a new value"
    #
    # I'm using * just in this example:
    test "${anArrayOfVariedSize[*]}" = "${*:10}" && echo correct
}

fourElements=( a1 a2 "a3 with spaces" a4 )
twoElements=( b1 b2 )
declare -A assocArray
assocArray[test]="works"

testPassingParams "first" "${fourElements[@]}" "${twoElements[@]}" "single with spaces" assocArray "and more... " "even more..."

test "${assocArray[inside]}" = "adding a new value"

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 was developed for that purpose.

Function.AssignParamLocally() {
    local commandWithArgs=( $1 )
    local command="${commandWithArgs[0]}"

    shift

    if [[ "$command" == "trap" || "$command" == "l="* || "$command" == "_type="* ]]
    then
        paramNo+=-1
        return 0
    fi

    if [[ "$command" != "local" ]]
    then
        assignNormalCodeStarted=true
    fi

    local varDeclaration="${commandWithArgs[1]}"
    if [[ $varDeclaration == '-n' ]]
    then
        varDeclaration="${commandWithArgs[2]}"
    fi
    local varName="${varDeclaration%%=*}"

    # var value is only important if making an object later on from it
    local varValue="${varDeclaration#*=}"

    if [[ ! -z $assignVarType ]]
    then
        local previousParamNo=$(expr $paramNo - 1)

        if [[ "$assignVarType" == "array" ]]
        then
            # passing array:
            execute="$assignVarName=( \"\${@:$previousParamNo:$assignArrLength}\" )"
            eval "$execute"
            paramNo+=$(expr $assignArrLength - 1)

            unset assignArrLength
        elif [[ "$assignVarType" == "params" ]]
        then
            execute="$assignVarName=( \"\${@:$previousParamNo}\" )"
            eval "$execute"
        elif [[ "$assignVarType" == "reference" ]]
        then
            execute="$assignVarName=\"\$$previousParamNo\""
            eval "$execute"
        elif [[ ! -z "${!previousParamNo}" ]]
        then
            execute="$assignVarName=\"\$$previousParamNo\""
            eval "$execute"
        fi
    fi

    assignVarType="$__capture_type"
    assignVarName="$varName"
    assignArrLength="$__capture_arrLength"
}

Function.CaptureParams() {
    __capture_type="$_type"
    __capture_arrLength="$l"
}

alias @trapAssign='Function.CaptureParams; trap "declare -i \"paramNo+=1\"; Function.AssignParamLocally \"\$BASH_COMMAND\" \"\$@\"; [[ \$assignNormalCodeStarted = true ]] && trap - DEBUG && unset assignVarType && unset assignVarName && unset assignNormalCodeStarted && unset paramNo" DEBUG; '
alias @param='@trapAssign local'
alias @reference='_type=reference @trapAssign local -n'
alias @var='_type=var @param'
alias @params='_type=params @param'
alias @array='_type=array @param'

How to use Positional parameters and special variables in Linux , We can pass variables into shell scripts in different ways to avoid hard coding of The option -l is a positional parameter for ls command. Is there a way to change the command line arguments in a Bash script. Say for example, a Bash script is invoked the following way: ./foo arg1 arg2 Is there a way to change the value of arg1 within the script? Say, something like . $1="chintz"

Shell Script Parameters, A bash shell script have parameters. These parameters start from $1 to $9. When we pass arguments into the command line interface, a positional parameter is� There are a couple of ways to do this. First, you could simply check to see if $1 contains anything like so: #!/bin/bash if [ "$1" != "" ]; then echo "Positional parameter 1 contains something" else echo "Positional parameter 1 is empty" fi

How to check if there are no parameters provided to a command , On the other hand # expands to the number of positional parameters. Example: For more details man bash and read topic named Special Parameters. I want a faster version, maybe written in C. Also, a version which is compatible with zsh. Maybe this deserves a separate question ("Is there a way to parse command-line arguments in Bash-like shells which accepts standard long-option syntax and doesn't require option names to be typed more than once?"). – Metamorphic Sep 9 '18 at 8:04

Linux Bash Command, These usually set the shell variables PATH, USER, MAIL, HOSTNAME and HISTSIZE. To avoid conflicts with parameter expansion, the string "${" is not considered others the positional parameters, may not be assigned this way, however. The arguments are accessible inside a function by using the shell positional parameters notation. When a function is executed, the shell script positional parameters are temporarily replaced inside a function for the function's arguments and the special parameter # is updated to expand to the number of positional arguments for the function.

Comments
  • What's the difference between using $1 vs $ARG1?
  • @JonLin: I can use more intuitive name, instead of $1, $2. The numbers are confusing.
  • Do you really require seven mandatory parameters? A common approach would be to turn as many as possible into options, and supply a default value where it makes sense. It makes your script a lot easier to use if you don't have to memorize a particular positional order for such a large number of arguments, even if most users would override the default values most of the time.
  • Hey @Bhushan! I think I have a solution for you. I've posted an answer: stackoverflow.com/questions/12128296/…
  • This question looks like : Specify command line arguments like name=value pairs for shell script
  • The 'local' method: copy from=/tmp/a to=/tmp/b; looks very similar to how one would call a Make target 'copy' and pass it two new environment variables (from and to). I really like the syntax similarities.
  • @claytontstanley: yes, that was the intent ;).
  • I use the global variable approach for optional arguments, but I name said variable after the function and clear it within the function after it's checked. For example, if I have an optional prompt for get_input(), then I'd use gi_prompt="blah blah blah", and in the function, unset gi_prompt. By namespacing everything related to the function, this prevents leaks and naming conflicts while allowing some flexibility.
  • When you implement the long parser example I think you need to shift 2 to move to the next argument pair.
  • Thanks. I will try it out.
  • @Bhushan I've just updated the code with one more addition - passing references, which was introduced in bash 4.3. Among other things it makes it possible to pass associative arrays into functions.