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

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


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/)


You mention BAT files so perhaps what you want is to write a shell script. If so then just enter the commands you want line-by-line into a file like so:

command1command2

and ask bash to execute the file:

bash myscript.sh

If you want to be able to invoke the script directly without typing "bash" then add the following line as the first line of the file:

#! /bin/bashcommand1command2

Then mark the file as executable:

chmod 755 myscript.sh

Now you can run it just like any other executable:

./myscript.sh

Note that unix doesn't really care about file extensions. You can simply name the file "myscript" without the ".sh" extension if you like. It's that special first line that is important. For example, if you want to write your script in the Perl programming language instead of bash the first line would be:

#! /usr/bin/perl

That first line tells your shell what interpreter to invoke to execute your script.

Also, if you now copy your script into one of the directories listed in the $PATH environment variable then you can call it from anywhere by simply typing its file name:

myscript.sh

Even tab-completion works. Which is why I usually include a ~/bin directory in my $PATH so that I can easily install personal scripts. And best of all, once you have a bunch of personal scripts that you are used to having you can easily port them to any new unix machine by copying your personal ~/bin directory.


it's probably easier to define functions for these types of things than aliases, keeps things more readable if you want to do more than a command or two:

In your .bashrc

perform_my_command() {    pushd /some_dir    my_command "$@"    popd}

Then on the command line you can simply do:

perform_my_command my_parameter my_other_parameter "my quoted parameter"

You could do anything you like in a function, call other functions, etc.

You may want to have a look at the Advanced Bash Scripting Guide for in depth knowledge.