Score:-1

Can alias be part of the string I type on the command line?

dm flag

Can I make an alias in the way that it is part of the string I type on the command line? In addition to that, I'd like that alias to have more than one value.

For example, if I enter any of the following strings on the command line:

env1_machineName
env2_machineName
env3_machineName

I'd like env1, env2 and env3 to be a single alias so that the same aliased command can be executed upon me typing in any of the three strings above.

So I'd like that the same command be executed if I on the command line type in env1, or env2 or env3 followed by anything else.

Artur Meinild avatar
vn flag
Please check Vanadium's answer, and tell if it answered your question. If not, please edit your question to make it more clear. It's very difficult to understand what you're actually asking. Please use more examples to illustrate what you want to do.
Score:0
cn flag

An alias is identified by its name, and has a single definition. Since it overrides commands with the same name, for sure, there will be no interference if the alias name is only part of an existing command. Short answer is thus: yes, alias be part of the string you type on the command line.

Set up three aliases, env1, env2 and env3.

alias env1="env1_machineName"
alias env2="env2_machineName"
alias env3="env3_machineName"
  • For daily use, an alias behaves like a command. Anything you type after env1, env2 or env3 will be passed to the command defined by the alias, (same_command in this case).
  • Beware that an alias is only effective in the current interactive shell. An alias exists in the current interactive shell only. An alias cannot be executed from within a script, even if defined there, unless the script is "sourced" within the current shell (source or .).

If in contrast, you want the tree aliases to execute the same command (since you also said)

| So I'd like that the same command be executed if I on the command line type in env1, or env2 or env3 followed by anything else.

then just define them three times to represent the same command:

alias env1="samecommand"
alias env2="samecommand"
alias env3="samecommand"
raj avatar
cn flag
raj
I have an impression that you are answering not the question OP asked. But the question was not very clear, so it's hard to understand, what was actually asked.
vanadium avatar
cn flag
At least, I attempted to answer the " Can alias be part of the string I type on the command line?" question.
user1696453 avatar
dm flag
@vanadium, thank you for your explanation. Maybe my question wasn't very clear. From what you wrote I assume that a single alias cannot be associated with, in this case, three different strings (env1, env2 and env3). Right? Because that's what I'd like to happen. When I type in env1_machineName or env2_machineName or env3_machine the same alias is executed.
vanadium avatar
cn flag
Three aliases can define the same command, so typing any of them will execute the same command: that is what I indicate in the second part of my answer. An alias is never executed: it is only an alias for a command.
user1696453 avatar
dm flag
Ok, still i don't think I was clear enough. Sorry for the confusion. Let me try be more precise: CMD input: env1_machineName should be handled by alias env="same_command", CMD input: env2_machineName should be handled by alias env="same_command", CMD input: env3_machineName should be handled by alias env="same_command" So, I want any CMD line input that contains env to be handled by the env alias. Hope it is clear now. Thanks for being patient.
vanadium avatar
cn flag
There is no use in clarifying your question in the comments. This here is a Q&A site for reference of future readers. It tries to collect good clear questions and good clear answers. Comments are there to improve both questions and answers. Use "edit" to clarify your question.
I sit in a Tesla and translated this thread with Ai:

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.