I know it for one command but how to work with a sequence?
Asked
Active
Viewed 2,713 times
2 Answers
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.
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.

Stéphane Chazelas
- 544,893

UVV
- 3,068
-
U Rocks!!! @UVV – sashi Nov 27 '14 at 09: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 09:34
-
@KasiyA as I said, imo with a function you have more flexibility. I extended my answer. – UVV Nov 27 '14 at 09:49
-
That's A function. and OP wants create ALIAS for a sequence of commands as @HaukeLaging answers. If you want to know what is different between a function and an alias, OK, you can run these command for test. Was the same as alias? – αғsнιη Nov 27 '14 at 09:57
-
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
-
@PM2Ring the answer for alias had already been given, I just showed how to do it with a function – UVV Nov 27 '14 at 11:11
-
Ah, ok, UVV. It's hard to tell these things when reading questions from a couple of hours ago. :) – PM 2Ring Nov 27 '14 at 11:19
6
start cmd:> alias foo="echo foo; echo bar"
start cmd:> foo
foo
bar

Hauke Laging
- 90,279
-
-
-
4May be better as
alias foo='{ echo foo; echo bar; }
or even better as a function (to avoid confusion if one runsfoo bar
) as otherwise you'd get surprising results when doingcmd || foo
orfoo | bar
orfoo > file
for instance. – Stéphane Chazelas Nov 27 '14 at 10:51