Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 805
stormist Avatar asked Jan 25 '10 21:01

stormist


People also ask

Can aliases be used to run multiple commands?

We can declare a function which allows us to run multiple commands under one alias.

How do you chain multiple commands in bash?

Ampersand Operator (&) Sixth Bash shell command line Chaining Operator (Linux operator) is Ampersand Operator (&). Ampersand Operator is a kind of operator which executes given commands in the background. You can use this operator to execute multiple commands at once.


2 Answers

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

like image 59
Larry Shatzer Avatar answered Sep 20 '22 05:09

Larry Shatzer


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:

command1 command2 

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/bash command1 command2 

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.

like image 24
slebetman Avatar answered Sep 21 '22 05:09

slebetman