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 8873239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:24:54+00:00 2026-06-14T18:24:54+00:00

Is it possible to call a function (defined in a shell script) from Unix

  • 0

Is it possible to call a function (defined in a shell script) from Unix console? I have a function like

add ()
{
 a=$1
 b=$2
 c=`expr $a + $b`
}

This is defined in the script "my_script.sh". How to call this function from command prompt?

  • 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-14T18:24:56+00:00Added an answer on June 14, 2026 at 6:24 pm

    To use arguments you don’t apply the redirection operator <, which redirects the file given right after the < to the standard input of your program. You need to make a call of your function in your script, in order to use the args $..., or you can simply load the function in your shell, and use it.

    In both cases, the call of your function is to be performed like this:

    add arg1 arg2
    

    If by command line, you must load the contents of your .sh file — your function definitions –, so that you may call them from your shell. In this one case, you file should look like:

    #! /bin/bash
    add () {    
        a=$1
        b=$2
        c=$(expr $a + $b)
        echo $c
    }
    

    And would use the function in your shell by doing the following:

    source script.sh
    add 100 1000
    

    If in your script.sh file, you use:

    ./script.sh arg1 arg2
    

    And the contents of the script file would be:

    #! /bin/bash
    add () {
    
        a=$1
        b=$2
        c=$(expr $a + $b)
        echo $c
    
    }
    add $1 $2
    

    Obs: your syntax was not valid in the function definition: you must embrace an expression in order to assign the result of it’s evaluation; the syntax for evaluating an expression may be either cmd or $(cmd), with the difference that the first approach allows no nesting calls.

    If you have more than one function being defined, you can either set the args for each function call, or simply define your functions, and load them up to your shell with “source script.sh”.

    Since you’ve loaded the functions, you need to call them as well as if they’ve been defined one by one. The args inside the function correspond to the classic “argv[]” parameters in C, they’re unique to the function call.

    For example, if you wanted to define an add and a sub function, you’d have:

    #! /bin/bash
    add () {
        a=$1
        b=$2
        c=$(expr $a + $b)
        echo $c
    }
    add $1 $2
    
    sub() {
        a=$1
        b=$2
        c=$(expr $a - $b)
        echo $c
    }
    sub $3 $4
    

    Or whatever argument numbers may fits your application.

    Notice, though, that you might lose track of the “semantic” of the vars: this can be caused by the dynamic scope of bash scripting, which shadows your variable definitions each time a call is performed.

    A straightforward example of this is a for loop. Consider the add function defined as follows:

    add () {
        let i=$1 + $2
    }
    

    And the for loop being performed as:

    for (( i = 0; i < 10; i++ )); do
        echo $i
        add i 5
        echo $i
    done
    

    The output is:

    0
    5
    6
    11
    

    since the variable “i” has been modified, during the for loop, not only by the “i++”, but by the function “add”, that has “i” being used in its scope.

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

Sidebar

Related Questions

Is it possible to call a function from one Module to another? I have
Is it possible to call a function defined in a non-specialised template class from
I want to call this function from OpenSSL library defined with prototy below: X509
Is it possible to call a function in tcl script which is aactually defined
I have a function defined like this: void doSomethingWithCustomer (const Customer &customer); One of
In the code below, is it possible to call the error function from the
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: How to call javascript function from c# Is there a way to
Is it possible to call a c(++) static function pointer (not a delegate) like
Possible Duplicate: CakePHP: Call to a member function find() on a non-object I have

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.