31

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,522
  • 2
  • 16
  • 28

3 Answers3

37

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,875
  • 2
  • 32
  • 55
  • 7
    for command1 && command2 command2 will only be executed if command1 is successful. – sourav c. Feb 01 '14 at 03:18
  • Nice discussions, relevant/similar posts: http://askubuntu.com/questions/334994/which-one-is-better-using-or-to-execute-multiple-commands-in-one-line http://stackoverflow.com/questions/13077241/execute-combine-multiple-linux-commands-in-one-line – gevang Feb 01 '14 at 04:04
  • @souravc : I made an edit, thanks, I learned something. – MrVaykadji Feb 01 '14 at 10:27
  • if combining sudo apt upgrade and sudo systemctl reboot, would you need to add 'sudo' twice, or will it 'remember' sudo for the second command? – Koen Jan 06 '19 at 21:09
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
  • 169,590
  • 1
    if true; then may be clearer to read if so desired. : may be confused with the ; at first glance. – kiri Feb 01 '14 at 10:45
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,246
  • 16
  • 81
  • 118
  • Is that the same that if :; then already mentionned ? Or is it slightly different ? – MrVaykadji Feb 01 '14 at 10:38
  • 1
    @MrVaykadji It's the same outcome, but a different method. if : runs a test on the null command, which will always return true. { .. } just groups the commands together. I personally find { .. } easier to remember. – kiri Feb 01 '14 at 10:39