-1

I am new to bash scripting and trying to find a way to call a bash function from another bash function that takes one or many arguments passed like we have in other languages ?

for example.

function b()
{
    echo "$1 World!"
}

function a()
{
    b("Hello!")
}

with call for function "a" would give output of Hello World! (I am not sure if this will work). Any help is appreciated.

Thank you

Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
Aamir
  • 21
  • 2
  • 1
    Start with searching the manual for "function" if you are new. There is no need to ask such trivial questions. – hek2mgl Nov 17 '16 at 15:13

1 Answers1

-1

Just add the parameters after the function call, separated by white space (Just like the parameters of a main function in a C or Java binary).

The following script:

#!/bin/bash

function b()
{
    echo "$1 World!"
}

function a()
{
    b "Hello!"
}

a

will output Hello! World!.

Surround the parameter with double-quotes if needed.

example:

$ b x y
x World!
$ b "x y"
x y World!
chepner
  • 497,756
  • 71
  • 530
  • 681
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105