How to execute several commands after each other with one request to the terminal (without using a file)?
You can separate commands with &&
or ;
.
&&
only runs the next command if the previous one exited with status 0 (was successful) :command1 && command2 && command3
;
runs every commands, even if the previous one exits with a non zero status :command1; command2; command3
You can combine these separators as you wish.
If you are interested to type each command on its own line in one single request you can use the following method:
Start your request (first line) with
if :; then
(this mean: if true, then do) and press Enter; your prompt will change now in>
and nothing will be executed.Type your commands, each one followed by Enter
Finish your request with with
fi
(end of the aboveif
condition) and press Enter. Now all your commands will be executed in the given order.
Example:
radu@Radu: ~ $ if :; then
> echo 'something'
> echo 'something else'
> echo 'List current directory contents:'
> ls
> echo 'Change current directory with root directory:'
> cd
> #finish
> fi
something
something else
List current directory contents:
Backups Desktop forma3d Public Untitled txt.txt~
bin Documente Music Templates Videos
configuration.php examples.desktop passwd~ tmp~
Downloads file~ Poze Ubuntu One
Change current directory with root directory:
radu@Radu: / $
First, put a {
on its own line.
Then, insert your commands.
Then, put a }
on a new line and press Enter. Your commands will be executed.
Example:
{
echo list
echo of
echo commands
echo to run at once
}
which will print (all at once, with no prompt in between):
list
of
commands
to run at once
As a side note, { .. }
is the Bash command grouping syntax. It's often useful in conjunction with &&
or ||
('and', and 'or' respectively)