Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003

    Unanswered: How to call a function


    Can anyone give me a simple example,of how to pass a value to a function and how to use a value returned from the function.

    I also need to know of how to write a function and return value from it.

    For example I want to write a function which checks for special characters then how should I write it.

  2. #2
    Join Date
    Jan 2004
    Bordeaux, France
    From bash man pages :
    Shell functions are a way to group commands for later execution
    using a single name for the group. They are executed just like a
    "regular" command. When the name of a shell function is used as a
    simple command name, the list of commands associated with that function
    name is executed. Shell functions are executed in the current shell
    context; no new process is created to interpret them.

    Functions are declared using this syntax:
    [ `function' ] NAME () { COMMAND-LIST; }

    This defines a shell function named NAME. The reserved word
    `function' is optional. If the `function' reserved word is supplied,
    the parentheses are optional. The BODY of the function is the
    COMMAND-LIST between { and }. This list is executed whenever NAME is
    specified as the name of a command. The exit status of a function is
    the exit status of the last command executed in the body.
    Note that for historical reasons, the curly braces that surround the
    body of the function must be separated from the body by `blank's or
    newlines. This is because the braces are reserved words and are only
    recognized as such when they are separated by whitespace. Also, the
    COMMAND-LIST must be terminated with a semicolon or a newline.

    When a function is executed, the arguments to the function become
    the positional parameters during its execution (*note Positional
    Parameters::). The special parameter `#' that expands to the number of
    positional parameters is updated to reflect the change. Positional
    parameter `0' is unchanged. The `FUNCNAME' variable is set to the name
    of the function while the function is executing.

    If the builtin command `return' is executed in a function, the
    function completes and execution resumes with the next command after
    the function call. When a function completes, the values of the
    positional parameters and the special parameter `#' are restored to the
    values they had prior to the function's execution. If a numeric
    argument is given to `return', that is the function's return status;
    otherwise the function's return status is the exit status of the last
    command executed before the `return'.

    Variables local to the function may be declared with the `local'
    builtin. These variables are visible only to the function and the
    commands it invokes.

    Functions may be recursive. No limit is placed on the number of
    recursive calls.
    The following function display its arguments and completes with status 1 if no called without argument.
    function DisplayArgs {
       for arg ; do
          echo "Arg: $arg"
       if [ $# -eq 0 ]
          then return 1
          else return 0
    DisplayArgs "$@"
    if [ $? -eq 0 ]
       # Proceed arguments
       echo "Argument(s) missing"

    The following function return a string converted to upper case :
    function ToUppercase {
       echo "$*" | tr '[a-z]' '[A-Z]'
    ToUppercase $str          # Display uppercase string
    upper=$(ToUppercase $str) # Get uppercase string

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts