2

This question already has an answer here:

I know it for one command but how to work with a sequence?

marked as duplicate by Gilles linux Nov 27 '14 at 22:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

8

I put this as an answer, because cannot format it in the comment properly

foo() {
   echo foo
   echo bar
}

> foo
foo
bar

Imho, you have more freedom with a function than with alias. At least you can format it properly.

Advanced Bash-Scripting Guide

In a script, aliases have very limited usefulness. It would be nice if aliases could assume some of the functionality of the C preprocessor, such as macro expansion, but unfortunately Bash does not expand arguments within the alias body. [2] Moreover, a script fails to expand an alias itself within "compound constructs," such as if/then statements, loops, and functions. An added limitation is that an alias will not expand recursively. Almost invariably, whatever we would like an alias to do could be accomplished much more effectively with a function.

  • U Rocks!!! @UVV – sashi Nov 27 '14 at 9:13
  • @UVV It doesn't create an alias. That is just a function which runs some commands when you call it. – αғsнιη Nov 27 '14 at 9:34
  • @KasiyA as I said, imo with a function you have more flexibility. I extended my answer. – UVV Nov 27 '14 at 9:49
  • 1
    @KasiyA As I see it, he wants to run two commands consequently. I suggested just another way to do this. – UVV Nov 27 '14 at 10:11
  • 3
    @KasiyA: How is a function not as flexible as an alias? Generally, a function is far more flexible than an alias. Eg, a function can accept arguments. Aliases are fine for simple things, but otherwise the last sentence quoted above from the Bash man applies. Certainly, the question asked about aliases, and to be complete UVV should have shown how to do it with an alias, and then given a function-based answer, with the support of that Bash man quote. – PM 2Ring Nov 27 '14 at 10:49
5
start cmd:> alias foo="echo foo; echo bar"
start cmd:> foo
foo
bar
  • A function could also be used instead of alias. – UVV Nov 27 '14 at 9:05
  • can u explain that @UVV – sashi Nov 27 '14 at 9:06
  • 3
    May be better as alias foo='{ echo foo; echo bar; } or even better as a function (to avoid confusion if one runs foo bar) as otherwise you'd get surprising results when doing cmd || foo or foo | bar or foo > file for instance. – Stéphane Chazelas Nov 27 '14 at 10:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.