How can I define a bash alias as a sequence of multiple commands?

stormist picture stormist · Jan 25, 2010 · Viewed 57.9k times · Source

I know how to configure aliases in bash, but is there a way to configure an alias for a sequence of commands?

I.e say I want one command to change to a particular directory, then run another command.

In addition, is there a way to setup a command that runs "sudo mycommand", then enters the password? In the MS-DOS days I'd be looking for a .bat file but I'm unsure of the linux (or in this case Mac OSX) equivalent.

Answer

Larry Shatzer picture Larry Shatzer · Jan 25, 2010

For chaining a sequence of commands, try this:

alias x='command1;command2;command3;'

Or you can do this:

alias x='command1 && command2 && command3'

The && makes it only execute subsequent commands if the previous returns successful.

Also for entering passwords interactively, or interfacing with other programs like that, check out expect. (http://expect.nist.gov/)