3

Not good at creating aliases but I have several commands to run and I want to do them all at once.

$ activator
$ clean
$ compile
$ run 9002

I would like to combine all of these commands into a single command like:

$ activate 9002

Normally I think I would know how to do this but the issue here is that it will need to wait until the previous command finishes and the prompt returns. Also, if I do an $ activate 9000 then the last command would run $ run 9000

Is there a way to do this in my alias?

4 Answers 4

3

You can combine multiple commands in an alias using a semicolon:

alias name_goes_here='activator; clean; compile; run'

Then you can use name_goes_here 9002.

Alternately, if you need something more complex, consider making a function instead. They're considerably more flexible.

Sign up to request clarification or add additional context in comments.

2 Comments

I used this multiple times. But now, I am getting error syntax error near unexpected token ';'
Same with me, it's throwing error like "bash: syntax error near unexpected token `;'"
3

And if you want to continue the latter commands only when the former has finished successfully use && instead of ; like this:

alias activate="activator && clean && compile && run"

1 Comment

Ok. The other answers weren't doing this so I was thinking maybe mine was not working properly.
0

For the general case, an alias is the wrong tool for this job.

It'll work in this specific case, because it's only the last command to which arguments are being passed and there's no conditional logic required, but a function is the safer approach.

For instance, a shell function with exactly equivalent use to the alias could be written like so:

activate() { activator; clean; compile; run "$@"; }

...but you could also add other steps to be performed after the command taking arguments, which an alias doesn't allow:

activate() { activator; clean; compile; run "$@"; cleanup; }

Comments

0

You can use 2 bash syntax system :

  • ";" to separate commands
  • "$1" as your first argument.

It gives you the following alias :

alias activate="activator;clean;compile;run $1"

Hope that help.

edit :

My bad. You can use $1 with functions, not aliases.

3 Comments

$1 doesn't work in aliases. If this alias appears to work when used, it's doing so only because $1 is expanding to an empty string, then all content after the alias (not just the first argument, but everything -- this being how aliases work) is being appended. (Thus, if it were used in a case where $1 were previously set to an unexpected value, it would behave surprisingly).
If you wanted to be able to pass only the first argument, rather than appending all remaining command-line content, you'd need to use a function, not an alias.
Indeed, my bad. I think about it after and couldn't correct my self. Thanks for the correction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.