Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:28:57+00:00 2026-06-10T05:28:57+00:00

I have to write a function in bash. The function will take about 7

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:28:58+00:00Added an answer on June 10, 2026 at 5:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a bash function which will take all the arguments sent
I'd like to write a function that would have some optional code to be
I have problem with fancybox. I want to write a function that will run
I have write hibernate createcriteria function in my project like this Hibernatesession.createCriteria(Salesman.class).add(Restrictions.ilike(email, email)).list(); The
I have to write a function that retrieves some information about some signal handling
I have to write a function that accepts a variable number of arguments, similar
I have to write a swap function for my bubble sort That's what I've
I have a function that calls out a read or write request on a
I'm trying to write a bash script that will download all of the youtube
I'm trying to write a simple bash script that accepts all arguments and interprets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.