I'm trying to create a function that creates a function. I want to "hardcode" an argument in the inner function based on what is passed into the outer function, e.g.
outer() {
inner() { doSomething --context=$1 $@ }
}
Given the above, on calling
outer foo
I want the inner function's definition to be as if I ran
inner() { doSomething --context=foo $@ }
In other words, interpolate the outer function's $1 for the definition of the inner function, but leave $@ to be interpolated when calling the inner function.
How can I achieve this?
Edit, for those wondering about my usecase:
I'm working with a command-line tool with subcommands, e.g.
doSomething dothis somearg
doSomething dothat
It always needs a context flag though, e.g.
doSomething --context foo dothis somearg
doSomething --context foo dothat
And usually once I'm in a context, my next several commands will use the same context.
So the ideal solution would allow me to set the context once and use the inner function like an alias that already knows its context:
outer foo
inner dothis somearg
inner dothat
This could have also been done with an environment variable, e.g.
alias inner="doSomething --context=$CONTEXT"
But in reality there are 2 flags not 1, and this (now with realistic args)—
outer gke_gke-xpn-1_europe-west1_europe-west1-s41l stats_service
inner dothis somearg
inner dothat
—is just a little more friendly to use (for me) than—
CONTEXT=gke_gke-xpn-1_europe-west1_europe-west1-s41l
NAMESPACE=stats_service
inner dothis somearg
inner dothat