Reputation: 193
Is there a way to make a bash alias (or function) with its name coming from a variable?
For instance, is it possible to do something along these lines:
create_alias_with_name() {
alias $1="echo a custom alias"
}
Or something along these lines:
create_func_with_name() {
$1() {
"echo inside a function with a variable name"
}
}
In other words, I would prefer to have some kind of function "factory" that can register functions for me. Is this possible or beyond the capabilities of Bash?
Upvotes: 5
Views: 1012
Reputation: 415
just in case, one may use a variable both as a part of the alias name and as a part of the alias command:
alias foo${var1}="bar${var2}"
Upvotes: 0
Reputation: 224864
Did you even try it? Your first example works fine.
You can make the second work by adding an eval
:
create_func_with_name() {
eval "$1() {
echo inside a function with a variable name
}"
}
Upvotes: 5