How to excute several commands with one command [duplicate]

Usually I need to exceute several commands every time Im at work, start local server, start docker bash, run frontend service...and I wish I could do all those stuff with one single command.

I tried using an alias, but the implementation I saw were more in the field of making long commands shorter, but I wasn't able to make an alias with several commands.

Any idea if is possible to run a series of commands one after the other with one single command?


You can chain several commands directly on the command line.

One option is to use a semicolon, like this:

command1; command2; command3

This will fire all 3 commands after each other, unconditionally.

You can also use a logical operator, like this:

command1 && command2 && command3

&& is an "and" operator, and in this case command2 will only execute if command1 is successful etc.

You can also use this construction in aliases (with no need for a script), like this:

alias mycommand='command1; command2; command3'

Running several commands is pretty simple thanks to a script. Create any text file you want (e.g. a hidden file into your home).

gedit ~/.myscript.sh

Put the following content

#!/bin/bash

<my command 1>
<my command 2>
...

Make the file executable.

chmod +x ~/.myscript.sh

Then you can run the script with the following command.

~/.myscript.sh

Finally, you can add an alias into your ~/.bashrc.

alias mycommand=~/.myscript.sh

So you can use mycommand to execute your script.