Score:0

First argument should convert to a variable and execute

cn flag

I have declared arg="echo demo"

When we execute the command my_command arg, then arg should be converted to $arg and should be executed like

my_command () {
    $arg
}

How do I call $1 as a variable?

Liso avatar
sd flag
Isn't what you have declared `arg="echo demo"` is a variable ?
tripleee avatar
nz flag
There is no way for your function to work correctly with quoted strings. Assigning a command to a string is probably the wrong thing to want to do anyway. Probably see https://mywiki.wooledge.org/BashFAQ/050
Score:2
us flag

The eval shell built-in does what you want:

$ help eval
eval: eval [arg ...]
    Execute arguments as a shell command.

    Combine ARGs into a single string, use the result as input to the shell,
    and execute the resulting commands.

So your function could look like this:

my_command() {
    eval $1
}

Instead of $1 you could also put $* and my_command will take an arbitrary number of arguments.

Note that by then my_command is little less than an alias for eval, so you could as well do:

alias my_command=eval
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.