leofelipe
leofelipe

Reputation: 33

How to call a function within another function in Shell Script?

Say I have this function:

function run_sanity_check(){
   echo -e "This is test level 1\n"
   echo -e "This is test level 2\n"
}

and this other function:

function run_test(){
  ... environment setup routine runs here
  echo -e "Running tests...\n"

  run_sanity_check --> this would be my call for the function above
}

When I call "run_test" I get this error: function: not found

Any help is appreciated.

Upvotes: 1

Views: 15173

Answers (1)

merlin2011
merlin2011

Reputation: 75545

As Brian's comment mentions, you need to use either function or () but not both.

Both of the following are valid, based on the documentation.

function run_sanity_check{
   echo -e "This is test level 1\n"
   echo -e "This is test level 2\n"
}


run_sanity_check(){
   echo -e "This is test level 1\n"
   echo -e "This is test level 2\n"
}

The following script prints expected output.

run_sanity_check(){
   echo -e "This is test level 1\n"
   echo -e "This is test level 2\n"
}

run_test(){
  echo -e "Running tests...\n"

  run_sanity_check 
}

run_test

Upvotes: 4

Related Questions