Have written a function named linge
. How can I use a replacement for the function name inside another function, locally using a different name such as lg
?
Asked
Active
Viewed 56 times
2 Answers
0
Simply define lg
to call ligne
.
function otherFunc() {
function lg() { ligne "$@"; }
lg 1 2 3
}
Note that this will define a global function lg
, too. See Achieve Local Function for how to work around this by defining the function to run in a subshell.
function otherFunc() (
function lg() { ligne "$@"; }
lg 1 2 3
)
But if you do this, any variable assignments done in the function will also not be visible outside the function.

Barmar
- 741,623
- 53
- 500
- 612
-
Have studied the link where the function is defined in a subshell. Can I use a subshell within a normal function using `{...}`? – Angio Sep 08 '21 at 22:05
-
-
I meant to say, a subshell with `(...)` within a normal function defined using `{...}`. – Angio Sep 08 '21 at 22:24
-
Of course. You can use that inside the function just like you can anywhere else. – Barmar Sep 08 '21 at 22:29
-
-
I am quite unsure about writing a function that can evaluate itself to `0` or `1`. Does one use `return 0` and `return 0` at the end or does one use `echo "0"` and `echo "1"` instead? – Angio Sep 08 '21 at 22:53
-
@Angio `return` sets the exit status of the function, which you can check in the caller using `if` or `$?`. `echo` writes output, which you can capture using `$(command)`. It depends on what the purpose of the function is. – Barmar Sep 09 '21 at 14:10
-
-
Why do you include "$@" as arguments to 'lg' in the declaration `function lg() { ligne "$@"; }`. Are the parameters "$@" taken from the parameters coming through `otherFunc`? – Angio Sep 09 '21 at 14:36
-
-
My assumption has been that "$@" in `lg` are tho parameter arguments for `lg`, rather than the parameter arguments set by `otherFunc`. The parameters to `otherFunc` could very well be different than the parameters to `lg`. – Angio Sep 09 '21 at 15:51
-
-
-
0
Have had a go using a subshell inside the function
myfunc ()
{
val="Fred"
(
function lg() { linge "$@"; }
lg "$src" && printf '%s\n' "No src used"
lg "$dst" && printf '%s\n' "No dst used"
val="Joe"
)
echo "val: $val"
echo "More commands follow"
}
Running the function myfunc
gives
val: Fred
The above means that val="Joe"
is local to the sub-shell. Would it be possible to got some variable inside the sub-shell that can be transferred outside the sub-shell (which will be used as a flag) for use by myfunc
.

Angio
- 57
- 1
- 5