0

In a shell script I want to call function f1_1 and the only way I found is the following.

#!/bin/bash

function f1 () {
    
function f1_1 () {
echo "Calling function f1_1"
}

echo "Calling function f1"
} 

f1
f1_1

My problem is that when I do it this way my output is the following

Calling function f1
Calling function f1_1

Is there a way to have an output with only the command inside f1_1 function

Calling function f1_1

I want this because function f1_1 have to be inside f1 function and I don't want to run the rest commands inside f1 function.

swa056
  • 95
  • 6
  • Do you mean to say that there are only certain conditions which you want `f1_1` to stop the execution of `f1`? If so, then make `f1_1` return something that `f1` can check via `$?` after `f1_1` returns. – Jeff Holt Apr 25 '21 at 14:48
  • I suppose you could run `f1 > /dev/null`, but what are you trying to do? In this case, `f1_1` isn't exactly *inside* `f1`, but it gets defined when `f1` is run... – xdhmoore Apr 25 '21 at 14:48
  • 5
    Can you explain why `f1_1` has to be defined inside `f1`? Why not define them separately, then you can call just `f1_1` without having to execute `f1` fist. – Socowi Apr 25 '21 at 14:53
  • 1
    Do the answers to ["How to define a function inside another function in Bash?"](https://stackoverflow.com/questions/8426077/how-to-define-a-function-inside-another-function-in-bash) help? – Gordon Davisson Apr 25 '21 at 17:18
  • @swa056 : Bash has not nested scope for functions. Once a function declaration is seen, the function is available from everywhere in the same process. – user1934428 Apr 26 '21 at 09:44
  • When I enter f1 function I run a case where one of the options is to run f1_1. My problem starts when I want to run f1_1 from outside f1 function but without running all the code inside f1 but only f1_1. – swa056 Apr 26 '21 at 18:45

0 Answers0