Skip to main content
added 3 characters in body
Source Link
user147505
user147505

Just to add a few words to the other answers. The function callback operates on function(s) external to the function that calls back. For this to be possible either a whole definition of the function to be called back needs to be passed to the function calling back, or its code should be available to the function calling back.

The former (passing code to another function) is possible, though I'll skip an example for this would involve complexity. The latter (passing the function by name) is a common practice, as the variables and functions declared outside of one function's scope are available in that function as long as their definition precedes the call to the function that operates on them (which, in turn, as to be declared before it's called).

Also note, that a similar thing happens when functions are exported. A shell that imports a function may have a framework ready and be just waiting for function definitions to put intothem in action. Function export is present in Bash and caused previously serious problems, btw (that was called Shellshock):

I'll complete this answer with one more method of passing a function to another function, which is not explicitly present in Bash. This one is passing it by address, not by name. This can be found in Perl, for example. Bash offers this way neither for functions, nor variables. But if, as you state, you want to have a wider picture with Bash as just an example, then you should know, that the function code may reside somewhere in the memory, and that code may be accessed by that memory location, which is called its address.

Just to add a few words to the other answers. The function callback operates on function(s) external to the function that calls back. For this to be possible either a whole definition of the function to be called back needs to be passed to the function calling back, or its code should be available to the function calling back.

The former (passing code to another function) is possible, though I'll skip an example for this would involve complexity. The latter (passing the function by name) is a common practice, as the variables and functions declared outside of one function's scope are available in that function as long as their definition precedes the call to the function that operates on them (which, in turn, as to be declared before it's called).

Also note, that a similar thing happens when functions are exported. A shell that imports a function may have a framework ready and be just waiting for function definitions to put into action. Function export is present in Bash and caused previously serious problems, btw (that was called Shellshock):

I'll complete this answer with one more method of passing a function to another function, which is not explicitly present in Bash. This one is passing it by address, not by name. This can be found in Perl, for example. Bash offers this way neither for functions, nor variables. But if, as you state, you want to have a wider picture with Bash as just an example, then you should know, that the function code may reside somewhere in the memory, and that code may be accessed by that memory location, which is called its address.

Just to add a few words to the other answers. The function callback operates on function(s) external to the function that calls back. For this to be possible either a whole definition of the function to be called back needs to be passed to the function calling back, or its code should be available to the function calling back.

The former (passing code to another function) is possible, though I'll skip an example for this would involve complexity. The latter (passing the function by name) is a common practice, as the variables and functions declared outside of one function's scope are available in that function as long as their definition precedes the call to the function that operates on them (which, in turn, as to be declared before it's called).

Also note, that a similar thing happens when functions are exported. A shell that imports a function may have a framework ready and be just waiting for function definitions to put them in action. Function export is present in Bash and caused previously serious problems, btw (that was called Shellshock):

I'll complete this answer with one more method of passing a function to another function, which is not explicitly present in Bash. This one is passing it by address, not by name. This can be found in Perl, for example. Bash offers this way neither for functions, nor variables. But if, as you state, you want to have a wider picture with Bash as just an example, then you should know, that the function code may reside somewhere in the memory, and that code may be accessed by that memory location, which is called its address.

Source Link
user147505
user147505

Just to add a few words to the other answers. The function callback operates on function(s) external to the function that calls back. For this to be possible either a whole definition of the function to be called back needs to be passed to the function calling back, or its code should be available to the function calling back.

The former (passing code to another function) is possible, though I'll skip an example for this would involve complexity. The latter (passing the function by name) is a common practice, as the variables and functions declared outside of one function's scope are available in that function as long as their definition precedes the call to the function that operates on them (which, in turn, as to be declared before it's called).

Also note, that a similar thing happens when functions are exported. A shell that imports a function may have a framework ready and be just waiting for function definitions to put into action. Function export is present in Bash and caused previously serious problems, btw (that was called Shellshock):

I'll complete this answer with one more method of passing a function to another function, which is not explicitly present in Bash. This one is passing it by address, not by name. This can be found in Perl, for example. Bash offers this way neither for functions, nor variables. But if, as you state, you want to have a wider picture with Bash as just an example, then you should know, that the function code may reside somewhere in the memory, and that code may be accessed by that memory location, which is called its address.