33

I could (1) prepare a file with typed commands separated by end-line, (2) make it executable, (3) run it from a file-system manager or the terminal.

But this is ridiculous for not repeatable and every-time-other sets of commands.

Can I type those commands to the terminal in one request instead?

I don't know end-line character for the terminal - Ctrl, Shift or Alt with Enter doesn't work.  

Asaf M
  • 291
Esamo
  • 1,542

3 Answers3

39

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.

MrVaykadji
  • 5,965
9

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 above if 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: / $
Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
4

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)

kiri
  • 28,986