How to make nested function local?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make nested function local?
# 1  
Old 06-22-2017
How to make nested function local?

Hi,

If I declare a function inside another function, it overwrites any previously declared function with the same name. This is NOT what I want.

Example:
Code:
#!/bin/bash

_test() { echo test; }

_myf() {
    # I'm using the same name as the other function.
    _test() { echo local test; }
    # I'm expecting this call to print "local test"
    _test
}

# I'm expecting this call to print "test"
_test
# I'm calling myf hopping it will print "local test" but leave function "_test" unchanged.
_myf
# I'm expecting this call to still print "test" but function _test has been irredeemably changed.
_test

What I hop to get is:
Code:
test
local test
test

What I get is:
Code:
test
local test
local test

How can I make sure that any function I declare inside a function stays local to the later function?

Thanks for you help.
Santiago
# 2  
Old 06-22-2017
Bash has local variables, but not local functions.
As a work-around you can try a classic sub-shell (where a modern implementation might avoid the overhead of an extra process).
Code:
_myf() {
    # I'm using the same name as the other function.
    (  # subshell start
    _test() { echo local test; }
    # I'm expecting this call to print "local test"
    _test
    )  # subshell end
}

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 06-22-2017
Thanks MadeInGermany,

I didn't think about your approach. And I didn't know there was no such local functions.

Because my function is pretty long, I'm going to stick to another workaround which is to use a rather unusual function name like in:

Code:
_myf() {
    _test_hopefully_no_one_ever_uses_that_name_ipjjebyfevljscsdgvsg() {
        echo local test
    }
    echo foo
    echo bar
    echo baz
    _test_hopefully_no_one_ever_uses_that_name_ipjjebyfevljscsdgvsg
    echo foo
    echo bar
    echo baz
    _test_hopefully_no_one_ever_uses_that_name_ipjjebyfevljscsdgvsg
}

# 4  
Old 06-22-2017
Hi.

The last example on page Functions suggests that bash cannot do this.

The shell ksh has a namespace facility. So if you wrote your code as in file user6:
Code:
#!/bin/ksh
#!/bin/bash

_test() { echo test; }

_myf() {
  # I'm using the same name as the other function.
  namespace mine {
    _test() { echo local test; }
    # I'm expecting this call to print "local test"
    _test
  }
}

# I'm expecting this call to print "test"
_test
# I'm calling myf hopping it will print "local test" but leave function "_test" unchanged.
_myf
# I'm expecting this call to still print "test" but function _test has been irredeemably changed.
_test

exit $?

then produces:
Code:
$ ./user6 
test
local test
test

For a system like:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
ksh 93u+

And, apparently if you needed to use the namespace function outside of the namespace, you could use it as .mine._test.

I have not used the namepace idea before, but it seems to work. See man ksh for some details.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 5  
Old 06-22-2017
Thanks drl,

Never used ksh before. Just gave your code a test and it does what I want.

Cheers
Santiago
This User Gave Thanks to chebarbudo For This Post:
# 6  
Old 06-22-2017
Hi, Santiago.

Good to hear that it worked for you.

Note that there may be some differences between ksh and bash, so keep that man page handy Smilie

Best wishes ... cheers, drl
# 7  
Old 06-22-2017
Does not work with the ksh on Solaris.
Solaris 11 ksh "Version JM 93u 2011-02-08" takes the namespace mine { }, but the _test inside it runs the global function.
However the .mine._test works.
And the namespace feature is not mentioned in the ksh man page.
--
Regarding the uniqueness of function names:
I would prefix the new function with the current function's name: _myf__test { }
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Programming

Local variable in a C function is not getting created in stack when its compiled with GCC

Hi, I am working in UEFI EDK2 Bios source. We created a platform related new package in the EDK2 source. I find a strange issue with the platform related code we added. When I did source level debugging I noticed the local variable in a C function is not getting created in stack when its... (6 Replies)
Discussion started by: Divya R
6 Replies

3. Shell Programming and Scripting

/usr/local/bin/expr function not working

Legends, I am not able to set "expr" function in ksh script. Below is the sample code i used, and output is as "Syntax error" Please help me to come out of it. OUTPUT (9 Replies)
Discussion started by: sdosanjh
9 Replies

4. Programming

Returning local string value from a function in C

Hi, If I have a code like this, what are the potential problems do you see? const char* const retString() { return "hello"; /* string literal */ } My questions are: a) Since the string literal which is already a constant read only data (cannot be... (4 Replies)
Discussion started by: royalibrahim
4 Replies

5. Programming

Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves? like thread_func() { int i, j; string... } Are the above local variables defined for each of... (1 Reply)
Discussion started by: saman_glorious
1 Replies

6. UNIX for Dummies Questions & Answers

make - foreach function

I wrote the following Makefile: dirs := a b c d files := $(foreach dir,$(dirs),$(wildcard $(dir)/*)) .PHONY: all all: touch $(files) The first two lines are taken from GNU make tutorial, Section 8.5 The foreach Function. I would expect the recipe touch $(files) to be... (2 Replies)
Discussion started by: ybelenky
2 Replies

7. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

8. UNIX for Dummies Questions & Answers

How to call a local function within Awk

Hi, I have the following statement which parses a string for me and prints it out: l_comp="dc000.runksh.test.ksh| $g_sql/dc0000.runksh_test.sql|new.dat|control.ctl" echo $l_comp | awk -F"|" '{ for ( i = 1; i <= NF; i++) { print $i; } } ' Rather then printing the data, I would like to... (5 Replies)
Discussion started by: CAGIRL
5 Replies

9. Shell Programming and Scripting

How to execute local function in awk

Hi All, Can you please tell me how to execute local function written in a shell script with awk. i tried with system command but its giving an error. (1 Reply)
Discussion started by: krishna_gnv
1 Replies

10. Shell Programming and Scripting

How to make variables in script function local?

Is it possible to make function variables local? I mean for example, I have a script variable 'name' and in function I have declared variable 'name' I need to have script's 'name' have the same value as it was before calling the function with the same declaration. The way to preserve a... (5 Replies)
Discussion started by: alex_5161
5 Replies
Login or Register to Ask a Question

Featured Tech Videos